For/If loop in cell array

2 Ansichten (letzte 30 Tage)
Bas
Bas am 14 Sep. 2015
Kommentiert: Bas am 14 Sep. 2015
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?

Akzeptierte Antwort

Stephen23
Stephen23 am 14 Sep. 2015
Bearbeitet: Stephen23 am 14 Sep. 2015
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 Kommentare
Stephen23
Stephen23 am 14 Sep. 2015
Bearbeitet: Stephen23 am 14 Sep. 2015
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 am 14 Sep. 2015
Thanks Stephen, I opened a new question.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 14 Sep. 2015
Anew = cellfun(@(V) V - (V>=1), A);

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by