Write matrix or array without overwriting

Hi,
I am getting a parameter called 'test' in a for loop. I want to write it to a matrix or array without overwriting. Each loop has a different dimension so that makes it a bit complicated.
I need on line h 'r' values of test, line h the new 'r' values of test and so on. So test needs to come in row h, column r.
for h = 3:10
...
...
...
A = []
for r = 1:g
...
for m = 1:numbcenterlines
...
percentage(m) = sum(distance>0 & distance <1) / numel(distance)*100;
test = max(percentage,[],'all');
end
A(r,h) = [A;test] %this part doesn't work well so my question is how to fix this part and get an array here with all the test values without overwriting
end
end

 採用された回答

Stephen23
Stephen23 2021 年 2 月 18 日

0 投票

You are mixing up two different approaches to adding data in a loop: concatenation and indexing. Just pick one, e.g.:
A = [A;test];

6 件のコメント

Dion Theunissen
Dion Theunissen 2021 年 2 月 18 日
編集済み: Dion Theunissen 2021 年 2 月 18 日
Thanks, on this way it is overwriting cause each 'h' needs a different row. now it takes the same column.
I need to add the 'test' in row h column r.
Walter Roberson
Walter Roberson 2021 年 2 月 18 日
A(r,h) = test;
Dion Theunissen
Dion Theunissen 2021 年 2 月 18 日
On this way it doesnt add it to the existing matrix. when it turns in the second loop it fills zeros on the earlier columns
Walter Roberson
Walter Roberson 2021 年 2 月 18 日
I do not understand what you want.
Maybe something like
A = {};
and later
A(r,h){end+1} = test;
???
Dion Theunissen
Dion Theunissen 2021 年 2 月 18 日
A =
0 0 67.9666
0 0 57.2263
0 0 73.3220
0 0 19.6101
0 0 21.3058
0 0 88.3791
0 0 78.7196
A =
0 0 0 36.5854
0 0 0 36.5854
0 0 0 36.5854
0 0 0 36.5854
0 0 0 36.5854
0 0 0 36.5854
0 0 0 36.5854
This is the result, but i want to save the third column aswell.
Walter Roberson
Walter Roberson 2021 年 2 月 18 日
Do not do the
A = [];
inside your outer loop.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by