フィルターのクリア

Produce matrices through for loop

2 ビュー (過去 30 日間)
Ali Esmaeilpour
Ali Esmaeilpour 2019 年 5 月 27 日
コメント済み: Ali Esmaeilpour 2019 年 5 月 31 日
Hello people! I want to produce N number of matrices while k=1:N and I want the result in a way like h(k). I mean h(1),h(2) etc which are matrices that depend on k. I mean creating them through a for loop. I tried the following code but it failed to produce N matrices and it just gave one matrix:
clc;
clear;
close all;
hx = [-1/sqrt(5);-2/sqrt(5)];
hu = [0;0];
N = 25;
Ek1 = zeros(N+1,1);
Ek2 = ones(N+1,1);
Ek3 = ones(N,1);
Ek4 = zeros(N,1);
h = zeros(102,1);
for k=1:N
if k==1
h(:,:) = [kron(Ek2,hx);kron(Ek3,hu)];
else
h(:,:) = [kron(Ek1,hx);kron(Ek4,hu)];
end
end
  1 件のコメント
Stephen23
Stephen23 2019 年 5 月 28 日
編集済み: Stephen23 2019 年 5 月 28 日
" I want to produce N number of matrices..."
Then the best** solution is to use indexing, exactly as your question already shows
** best in the sense simplest, neatest, easiest to write, easiest to debug, and most efficient.
Note that dynamically access variable names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:

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

採用された回答

Geoff Hayes
Geoff Hayes 2019 年 5 月 27 日
Ali - if the output matrix of each iteration is of dimension 102x1, then you could store each output as a column in a 102xN matrix. For example,
h = zeros(102,N);
for k=1:N
if k==1
h(:,k) = [kron(Ek2,hx);kron(Ek3,hu)];
else
h(:,k) = [kron(Ek1,hx);kron(Ek4,hu)];
end
end
  21 件のコメント
Geoff Hayes
Geoff Hayes 2019 年 5 月 31 日
For the case where N is 5, then
h = zeros((2*N + 1) * size(hx,1), N);
h is a 22x5 array. Is this correct?
Ali Esmaeilpour
Ali Esmaeilpour 2019 年 5 月 31 日
yeah tnx my friend i debugged that main code and solve it with sedumi. tnx again for you consideration. cheers!

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

その他の回答 (0 件)

カテゴリ

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