How to store the results from an iteration process?

1 回表示 (過去 30 日間)
Andes
Andes 2017 年 1 月 31 日
コメント済み: Andes 2017 年 2 月 1 日
Hello,
I need to store the results from a iteration similar to this: clear all A = (1:10)'; for i = 1:2 for x=[5 11] B = A * x; end C(:,i) = B; end but here only the last result is stored. I need this: C =[A*5,A*11] but with an iteration process similar to the above code. Any suggestions... Thank you very much in advance!

採用された回答

Niels
Niels 2017 年 1 月 31 日
maybe this does what you want
clc
clear all
A = (1:10)';
x=[5 11];
C=zeros(length(A),length(x));
for i = 1:2
C(:,i) = A * x(i);
end
if it does, you forgot to index x, you had 1 loop to much. for any i x would be 5 and 11.
and you forgot to preallocate C
  1 件のコメント
Andes
Andes 2017 年 2 月 1 日
Thank you very much, this is what i needed

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

その他の回答 (1 件)

Roger Stafford
Roger Stafford 2017 年 1 月 31 日
You have one too many for-loops
clear all
A = (1:10)';
x = [5,11];
for i = 1:2
C(:,i) = A * x(i);
end
  1 件のコメント
Andes
Andes 2017 年 1 月 31 日
Great, Thanks a lot!

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

カテゴリ

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