MATLAB Answers

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

I don't want to repeat these , I want to use a loop for it

1 ビュー (過去 30 日間)
jack joke
jack joke 2019 年 10 月 17 日
終了済み: MATLAB Answer Bot 2021 年 8 月 20 日 18:35
I don't want to repeat these all time , I want to use a loop for it instead of copy and paste it all time ?
Can I just use a ( for ) for it ??????
KG1=zeros (28,28);
KG2=zeros (28,28);
KG3=zeros (28,28);
KG4=zeros (28,28);
KG5=zeros (28,28);
KG6=zeros (28,28);
KG7=zeros (28,28);
KG8=zeros (28,28);
KG9=zeros (28,28);
KG10=zeros (28,28);
  1 件のコメント
Stephen
Stephen 2019 年 10 月 18 日
Numbering variables is a sign that you are doing something wrong.
Copy-and-pasting code is a sign that you are doing something wrong.
You can easily avoid both of these issues by using indexing. Indexing is simple, neat, easy to debug, and very efficient (unlike what you are trying to do).

回答 (1 件)

Kevin Phung
Kevin Phung 2019 年 10 月 17 日
編集済み: Kevin Phung 2019 年 10 月 17 日
perhaps instead of defining 10 variables or dynamically creating them in a for loop, you can store each matrix of zeros as a cell array.
KG = {zeros(28,28)};
KG = repmat(KG,10,1);
% now you can call out each cell array like so:
KG{1};
KG{2};
KG{3};
%etc...
  1 件のコメント
jack joke
jack joke 2019 年 10 月 17 日
Thanks bro for your answering I just found the answer in another way.
it was
for i=1:10
eval(['KG' num2str(i) '= zeros (28,28)']);
end

Community Treasure Hunt

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

Start Hunting!

Translated by