2017-07-06 5 views
0

私のforループでは、1回の印刷と17個の要素からの描画ではなく、17個の要素から同じベクトルが17回繰り返されます。何がうまくいかないのですか?forループは各繰り返しで同じものを印刷します。一度だけ印刷する必要があります

また、逆ベクトルの終わりに平均値を追加しようとしていますが、寸法がオフであると言っています。 (2番目の関数は動作しますが、ProcessSpikeの内部にあるので、2番目の関数は動作しますが、参照用に含めました)。

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 
+0

[mcve]を指定できます。つまり、すべての入力変数を定義できますか。 – m7913d

+0

forループ内で何を印刷しますか? – m7913d

+0

私は、17の異なる要素に対して、特定のクラスタの平均発火率のリストを印刷したいと考えています。だから、以下のようにrとmeanになるはずですが、代わりに私は同じことを17回取得します。 – Sophie

答えて

0

プリントが終わる;が欠落しているラインによって引き起こされる、あなたのエディタは、これらの行(警告)の下にオレンジ色の線を引く必要があります。 不一致のディメンションに関して、既存の配列(r(end+1) = num2str(mean))に文字列(char配列)を追加しようとしています。その文字配列の長さがrの他の要素の長さと一致しない場合、このようなエラーが発生します。ここではnum2str()を使用せず、値の文字列表現の代わりに単一の値をプッシュすることをお勧めします。

0

あなたのコードの改訂版にコメントを追加しました。うまくいけば分かりやすくなります。

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! 
関連する問題