Need helping in setting a for loop for the below code

조회 수: 1 (최근 30일)
Kevin Akash Rajasekaran
Kevin Akash Rajasekaran 2021년 5월 5일
답변: David Hill 2021년 5월 5일
Hey all! So I'm implementing a code which essentially runs through the length of condIds (which is 1X1070 double) as follows. For further context to what the variable refer to, condIds refer to the image file used for a particular trial. I'm retrieving all possible trials in a bunch of files for one single condId (denoted by myTrials).
for ii = 1:length(condIds)
myTrials = find(condIds == condIds(ii)); % all trials matching the condition on trial ii
end
When I run the following code, it only loads myTrials for the last condId i.e 1070. WHy does it not iterate through each condId? Thanks in advance!

답변 (1개)

David Hill
David Hill 2021년 5월 5일
u=unique(condIds,'stable');%not sure if you only want to go through the unique entries; otherwise, you will have duplicates
for ii = 1:length(u)
myTrials{ii} = find(condIds == u(ii)); %need to index into cell array
end

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by