Saving data into corresponding arrays of a cell array in cycles
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Jan
2019-4-25
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 个评论
Jan
2019-4-29
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!