How to store indices of for loop in cell array after satisfying a condition

조회 수: 1 (최근 30일)
Hi Mathworkers,
I am having problem in storing indices (That will serve as necessary data in my purpose)of for loops in a cell array after satisfying a condition. Here, there is a draft of the code.
for i=8:110
for j=34:110
for k=95:110
EE=myfunction(i,j,k); %% Function may be any
if EE<=5
(#######)
end
end
end
end
#### at this point, I need the cell array which stores the indices i,j,k for whom the condition (EE<=5) is satisfied.
output may be something like this,
9,50,99
9,61,103
..........
10,34,96
10,35,95 etc..

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 5월 3일
편집: KALYAN ACHARJYA 2019년 5월 3일
l=1;
m={};
for......
if EE<=5
(#######)
m{l}=[i,j,k];
l=l+1;
end
Check this way:
>> m={}
m=
{}
>> m{1}=[1,2,3];
>> m{2}=[2,2,7];
>> m
m=
[1x3 double] [1x3 double]
>>
You can acess any cell array, like
>> m{2}
ans =
2 2 7
One suggestion: If possible reduce the nesting of loops to make the code more efficeint.
  댓글 수: 2
Anupam  Saikia
Anupam Saikia 2019년 5월 3일
Thanks Mr. Kalayan Acharya. Can a called function be used in arrayfun?
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 5월 3일
편집: KALYAN ACHARJYA 2019년 5월 3일
Adaroni Saikia deb, you can do that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by