Simple for loop problem

1 回表示 (過去 30 日間)
Zeeshan Ahmad Khan
Zeeshan Ahmad Khan 2019 年 2 月 19 日
コメント済み: Stephen23 2019 年 2 月 19 日
Hi hope an expert can provide an answer to this trivial problem:
I have a problem with this nester for loop which I am posting here:
eta = [1e-3:1e-2:9e-1];
HN =5;
for ii = 1:numel(eta)
for v = 1:HN
Delta(v) = eta(ii)*6;
end
end
This code gives the output of DeltaEta as a 1x5 vector. However, I want the result to be 90x5 vector where DeltaEta is computed 5 times for each value of eta.
With these lines of code, the output is incorrect.
I believe the problem is with the way I am initializing the loops.
Any leads would be appreciated.
  1 件のコメント
Stephen23
Stephen23 2019 年 2 月 19 日
Simpler:
repmat(6*eta(:),1,HN)

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

採用された回答

KSSV
KSSV 2019 年 2 月 19 日
編集済み: KSSV 2019 年 2 月 19 日
eta = [1e-3:1e-2:9e-1]; m = length(eta) ;
HN =5;
DeltaEta = zeros(m,HN) ;
for ii = 1:m
for v = 1:HN
DeltaEta(ii,v) = eta(ii)*6;
end
end
Or Simply use:
DeltaEta = repmat(eta'*6,1,5) ;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by