How do I use if elseif to find indices of elements that meet a statement and assign NaN to elements that don't?
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
KSSV
on 16 Jun 2021
If a is your matrix, to replace the values which are less than -0.1 just use:
idx = a < -0.1 ;
a(idx) = NaN ;
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!