for循环在每次迭代中打印相同的东西,应该只打印一次
问题描述:
我遇到了麻烦,其中我的for
循环从17个元素中打印17次相同的矢量,而不打印1次并从17个元素中打印。出了什么问题?for循环在每次迭代中打印相同的东西,应该只打印一次
此外,我试图在倒数矢量的末尾添加平均值,但它表示尺寸已关闭。 (第二个函数可以工作,但是我将它作为ProcessSpike包含在内以供参考)。
function [] = ProcessSpike(dataset,element,cluster)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
result = []
for a = 1:element
for b = 1:cluster
result = [result AvSpike(dataset, a, b)];
mean = nanmean(result)
r = [result]'
r(end+1) = num2str(mean)
end
end
function [result] = AvSpike(dataset,element,cluster)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
Trans1 = dataset.Trans1;
Before_Trans1 = Trans1-600;
Firing_Time1 = dataset(cluster).time(dataset(cluster).time>Before_Trans1(element)&dataset(cluster).time<Trans1(element));
ISI1 = diff(Firing_Time1);
result = numel(ISI1)/600
result(result == 0) = NaN
end
答
的版画是由缺少结束;
线造成的,你的编辑器应该画在这些线路(警告)一条橙色的线。 关于不匹配的维度,您正试图将字符串(char数组)添加到现有数组(r(end+1) = num2str(mean)
)。如果该字符数组的长度与r
中其他元素的长度不匹配,则会导致此类错误。我建议在这里不要使用num2str()
,只需推送一个值而不是值的字符串表示。
答
我已经添加了对您的代码的修订版本的评论,希望能让事情更清楚。
result = []
for a = 1:element
for b = 1:cluster
% Concatenate vertically (use ;) so no need to transpose later
result = [result; AvSpike(dataset, a, b)];
% Use a semi-colon at the end of line to supress outputs from command window
% Changed variable name, don't call a variable the same as an in-built function
mymean = nanmean(result);
% r = result' % This line removed as no need since we concatenated vertically
% Again, using the semi-colon to supress output, not sure why num2str was used
r(end+1) = mymean;
end
end
disp(r) % Deliberately output the result!
你可以提供一个[MCVE],即定义所有的输入变量 – m7913d
你想你的内部打印的for循环是什么? – m7913d
我想打印17个不同元素的给定群集的平均点火速率列表。所以它应该在r上并且意思如下,但是我得到17次同样的结果。 – Sophie