How do I use if elseif to find indices of elements that meet a statement and assign NaN to elements that don't?
조회 수: 2 (최근 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에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!