Saving data into corresponding arrays of a cell array in cycles

5 visualizaciones (últimos 30 días)
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

Jan
Jan el 25 de Abr. de 2019
What exactly is the problem?
A = cell(N, 3); % Pre-allocate!!!
for i=1:N
if (a condition)
if (Condition 1)
A{i, 1} = some calculation;
elseif(Condition 2)
A{i, 2} = some calculation;
elseif(Condition 3)
A{i, 3} = some calculation;
end
end
end
Or maybe you want to add the elements without potential gaps:
A = cell(N, 3); % Pre-allocate!!!
iA = zeros(1, 3); % An index for each column
for i=1:N
if (a condition)
if (Condition 1)
iA(1) = iA(1) + 1;
A{iA(1), 1} = some calculation;
elseif(Condition 2)
iA(2) = iA(2) + 1;
A{iA(2), 2} = some calculation;
elseif(Condition 3)
iA(3) = iA(3) + 1;
A{iA(3), 3} = some calculation;
end
end
end
  3 comentarios
Jan
Jan el 29 de Abr. de 2019
For 1) The iterative growing of arrays is a DON'T in programming, because it needs an exponentially growing amount of resources. Example: If you create the array 1:1000 iteratively:
v = [];
for k = 1:1000
v = [v, k];
end
Matlab has to allocate sum(1:1000)*8 bytes of memory and copy almost the same amount: more than 2GB. Allocating too much memory is much cheaper. In your case start with N rows, and crop the unused memory at the end:
A = cell(N, 3); % Pre-allocate!!!
iA = zeros(1, 3); % An index for each column
for i = 1:N
...
end
A = A(1:max(iA), :); % Crop ununsed memory
For 2) "one line calculation outside of the if loop" There are no "if-loops" in any programming language I know. Do you mean the loop or the if command? Repeating which equation? Thje terms "if (a condition)" and "if (Condition 1)" are not useful to discuss the details. Please post the real code to clarify, what you want to achieve.
Sreekanth Nandakumar
Sreekanth Nandakumar el 3 de Mayo de 2019
Thank you very much for the information. I am unable to post the code here as it is bound by confidential data agreement and I am not allowed to post it on a public platform. Anyway I understand your information . Once again, thank you very much.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by