Full change of array?

1 回表示 (過去 30 日間)
JamJan
JamJan 2019 年 7 月 18 日
回答済み: madhan ravi 2019 年 7 月 18 日
Hi,
I have this little loop:
count = 0;
for i=1:3
for j=1:2
for k=1:3
count(count+1) = count + 1;
A = i;
SG(count) = A;
end
end
end
Why does it change everything to a 3 in the end, instead of keeping the numbers e.g. [1 1 1 1 1 2 2 2 2 3 3 3]. In other words, how can I make sure it does not change every number in SG, but keep the example array.
Thanks!

採用された回答

Bruno Luong
Bruno Luong 2019 年 7 月 18 日
You make a mixing array/scalar in your code. Change
count(count+1) = count + 1;
to
count = count + 1;

その他の回答 (1 件)

madhan ravi
madhan ravi 2019 年 7 月 18 日
You don't need a loop at all !:
repelem(i,numel(j)*numel(k)-1:-1:numel(i)) % according to your output in the example
% or perhaps you need
repelem(i,numel(j)*numel(k))

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by