How do I use if elseif to find indices of elements that meet a statement and assign NaN to elements that don't?
3 ビュー (過去 30 日間)
古いコメントを表示
Hi All,
I have a 3D matrix (a2).
I want to find the index of elements in a2 <= -0.1 in the first dimension for all trials (trl) and experiments (exp). Some of my trials (trl) don't have values <=-0.1, and for these I would like to assign them with NaN.
I've had a go at writing code, but my if elseif statement is completely off. Can someone please give me some pointers on what I should do.
Many thanks
onsetT=zeros(trl,anim); %preallocate
for exp = 1:12 %experiments
for trl=1:50 %trials
if a2(:,trl,exp)<=-0.1 %if there are elements <=0.1
[row] = find(a2(:,trl,exp)<=-0.1,1); %find index of these elements
onsetT(trl,exp)=lfpTime(row); %use index to locate onset time
elseif a2(:,trl,exp)>=0 %if elements >0 i.e. positive
a2(:,trl,exp)=NaN; %assign them with NaN
onsetT(trl,exp)=a2(:,trl,exp); %assign onset time for these trials NaN
end
end
end
0 件のコメント
回答 (1 件)
KSSV
2021 年 6 月 16 日
If a is your matrix, to replace the values which are less than -0.1 just use:
idx = a < -0.1 ;
a(idx) = NaN ;
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!