Basic question about loops

7 ビュー (過去 30 日間)
Pelajar UM
Pelajar UM 2021 年 10 月 29 日
コメント済み: Stephen23 2021 年 10 月 31 日
Very basic question: why is this not working?
instead of
Idx2=Idx(:,2)
Idx3=Idx(:,3)
Idx4=Idx(:,4)
Idx5=Idx(:,5)
I wrote
for i=2:5
Idx(i)=Idx(:,i)
end
The error is: Unable to perform assignment because the left and right sides have a different number of elements.
  10 件のコメント
Pelajar UM
Pelajar UM 2021 年 10 月 30 日
編集済み: Pelajar UM 2021 年 10 月 31 日
Perfect! Thank you so much.
Small error: your code mixes k and i.
Stephen23
Stephen23 2021 年 10 月 31 日
"Small error: your code mixes k and i"
Fixed, thank you!
If the content of C are of suitable sizes then after the loop you could concatenate them back into one array, e.g.:
A = cat(3,C{:})

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

採用された回答

millercommamatt
millercommamatt 2021 年 10 月 29 日
You could make use of genvarname, but this would work too.
for i = 2:5
eval(['Idx' num2str(i) ' = Idx(:,' num2str(i) ');']);
end
When you start using functions like genvarname and eval, it's usually a sign that you should be approaching your task in a different way.
  4 件のコメント
Pelajar UM
Pelajar UM 2021 年 10 月 29 日
Wonderful. Thanks a lot.
Stephen23
Stephen23 2021 年 10 月 29 日
"Yes, this is exactly what I wanted"
and is a slow, complex, inefficient, obfuscated approach this task.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by