Matlab for循环在每次迭代中打印相同的内容,应该只打印一次
我遇到问题,我的Matlab for循环在每次迭代中打印相同的内容,应该只打印一次,matlab,for-loop,Matlab,For Loop,我遇到问题,我的for循环从17个元素打印同一矢量17次,而不是从17个元素打印1次和绘图。出什么事了 另外,我试图在反转向量的末尾加上平均值,但它表示维度是关闭的。(第二个函数可以工作,但我将其包含在ProcessSpike中以供参考) function[]=ProcessSpike(数据集、元素、集群) %此函数的无标题摘要如下所示 %这里有详细的解释 结果=[] 对于a=1:元素 对于b=1:cluster 结果=[结果AvSpike(数据集,a,b)]; 平均值=平均值(结果) r=[结
for
循环从17个元素打印同一矢量17次,而不是从17个元素打印1次和绘图。出什么事了
另外,我试图在反转向量的末尾加上平均值,但它表示维度是关闭的。(第二个函数可以工作,但我将其包含在ProcessSpike中以供参考)
function[]=ProcessSpike(数据集、元素、集群)
%此函数的无标题摘要如下所示
%这里有详细的解释
结果=[]
对于a=1:元素
对于b=1:cluster
结果=[结果AvSpike(数据集,a,b)];
平均值=平均值(结果)
r=[结果]'
r(末端+1)=num2str(平均值)
结束
结束
函数[结果]=AvSpike(数据集、元素、群集)
%此处是此函数的无标题摘要
%这里有详细的解释
Trans1=dataset.Trans1;
_Trans1之前=Trans1-600;
触发_Time1=dataset(cluster).time(dataset(cluster).time>在_Trans1(元素)和dataset(cluster)之前。time打印是由缺少结尾的行引起的,编辑器应该在这些行下面画一条橙色线(警告)。
关于不匹配的维度,您正在尝试向现有数组(r(end+1)=num2str(mean)
)添加字符串(char数组)。如果该char数组的长度与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!
你能提供一个,即定义所有输入变量吗?你想在for循环中打印什么?我想打印一个给定集群17个不同元素的平均触发率列表。因此它应该在r上,如下所示,但我得到了17次相同的结果。result=NaN-mean=1.7186 r=NaN-NaN-N安楠0.0050楠楠2.3067楠楠0.1300楠楠0.4967 0.0350楠楠10.8767楠(因字符空间有限而缩写)您可以对问题进行编辑,而不是在注释中使用缩写和未格式化的代码!
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!