Saving data into corresponding arrays of a cell array in cycles

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

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

Sreekanth Nandakumar
Sreekanth Nandakumar el 25 de Abr. de 2019
Editada: Jan el 29 de Abr. de 2019
The second method almost fits my purpose . I need to know two things.
1) My N value is 250 and not all the values satisfy the main 'if condition'. So each array in the cell array has only 20 rows. So if I pre allocate the cell array for N (250) rows, only the first 20 is filled up and the remaining 230 rows are saved with blank cells[]. Is there any way to just dynamically add rows, each loop when the conditions actually satisfy? Right now it saves like this.
10 20 30
100 200 300
[] [] []
[] [] []
- etc - -
2) Is there any way of using just one line calculation outside of the if loop rather than repeating the equation at each condition. The equation is exactly the same in all the conditions.
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.
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 Centro de ayuda y File Exchange.

Preguntada:

el 25 de Abr. de 2019

Comentada:

el 3 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by