Saving data into corresponding arrays of a cell array in cycles
Mostrar comentarios más antiguos
Hello all,
I have a code as generalized below. What I need to do is for each loop only one if/elseif condition satisfy and the value of A should be saved in a matrix. But I need to save all the 3 conditions in different arrays of a cell array. Adding to that the conditions repeat many times and the array where the values are saved should correspond to each condition. To reduce the confusion , I will summarize below.
(1) For example in the first loop where i=1 the condition 3 satisfies - So I need to save the value of A in a cell array. Say A=10
(2) Next when i=2, the condition 2 satisfies and now the value of this A should be saved in the second column of the cell array. say A=20
(3) and same applies when i=3 and calculation in the condition 1 should be saved in the 3rd column. say A=30
(4) Now in the next loop when i=4, (1) repeats, that is the condition 3 is true. In this case I need to save the value of A below the column of (1). This is a cycle which repeats (1), (2) and (3) many times. Consider the values of A in steps (1), (2) and (3) in the second cycle as 100,200 and 300, my cell array after the second cycle should be saved as below
{1x2} {1x2} {1x2}
10 20 30
100 200 300
I can save the values in different matrices by defining different variable names for each 'if condition' (For eg: A1,A2,A3) but that is not what I want. Moreover I have many more variables to calculate in each condition and the number of if/elseif statements are more than shown here in the generalized version.
for i=1:N
if (a condition)
if (Condition 1)
A=some calculation;
elseif(Condition 2)
A=some calculation;
elseif(Condition 3)
A=some calculation;
end
end
end
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!