Hi,
I have a question regarding implementing a for and if loop with cell arrays.
I have a matrix A{1x24 cell} Each cell contains ages of people, but each cell has a different length (i.e. A{1} is 2362x1, while A{2} is 2344x1, etc.).
Now I want to apply an if loop that for each value in A which is >=1, that specific value will be subtracted by 1.
Can anyone help me with this?

 채택된 답변

Stephen23
Stephen23 2015년 9월 14일
편집: Stephen23 2015년 9월 14일

0 개 추천

Some fake data to try, and the loop:
A{3} = 2:5;
A{2} = -3:4;
A{1} = -2:2;
for k = 1:numel(A)
idx = A{k}>=1;
A{k}(idx) = A{k}(idx) - 1;
end

댓글 수: 3

Bas
Bas 2015년 9월 14일
편집: Bas 2015년 9월 14일
Thanks! This works..
One more question:
Now I have this updated matrix A as described above. I have a matrix B containing matrices, while A contains cells.
Matrix B contains ages from 26 up to 56 in the first column. The second column defines their salary.
I want to create a matrix C which contains the corresponding salaries of all people that are specified in A.
So something like this:
if floor(value in A) == B(:,1)
C(value)= B(value,2);
end
Stephen23
Stephen23 2015년 9월 14일
편집: Stephen23 2015년 9월 14일
Please ask this in a new question, giving both input and output example data for us to try. Currently it is not clear what "B containing matrices" means, ditto "The second column defines their salary", etc. Is the salary "defined" as a value, a code, a category, a name, an index, or something else?
Without example data arrays we cannot try our code and we cannot test our code.
Bas
Bas 2015년 9월 14일
Thanks Stephen, I opened a new question.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 9월 14일

0 개 추천

Anew = cellfun(@(V) V - (V>=1), A);

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

Bas
2015년 9월 14일

댓글:

Bas
2015년 9월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by