array length is not what I've set...

4 ビュー (過去 30 日間)
beginner
beginner 2016 年 3 月 23 日
編集済み: Jos (10584) 2016 年 3 月 23 日
Hi,
This is my code, it works just fine, just I dont understand why my array Hopt_new is generating more arrays than I set.
Bo=1;
X = 0:0.1:3;
Hopt_new = zeros([1 90]);
h0_new = 0;
for j = 90:1:179
theta = j*pi/180;
handle = @(h0) test2fun(h0, theta, Bo,X);
Hopt_new(j)=fsolve(handle, h0_new);
h0_new = Hopt_new(j);
end
Why does Hopt_new have length of 179 instead of 90, which I set before the for loop? Is this because j goes until 179? I mean j actually is a "vector" with length of 90. I just dont get it.
Thanks a lot.
  1 件のコメント
Jos (10584)
Jos (10584) 2016 年 3 月 23 日
Yes, take a look at this code, and then see my answer:
A = [1 2 3]
A(6) = 99

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

採用された回答

random09983492
random09983492 2016 年 3 月 23 日
The first time the loop executes it sets Hopt_new(90).
The second time it sets Hopt_new(91).
If you want to start at the first index of Hopt_new, assign Hopt_new(j-89) rather than just Hopt_new(j).

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2016 年 3 月 23 日
編集済み: Jos (10584) 2016 年 3 月 23 日
In your code, the variable j has two roles:
  1. an index into Hopt
  2. a value that is used for calculation (angle?)
Your best option is to disentangle these using two variables:
Bo = 1;
X = 0:0.1:3;
Hopt_new = zeros([1 90]);
h0_new = 0;
Angle = 90:1:179 ;
for j2 = 1:numel(Angle)
% loop over all angles
theta = Angle(j2) * pi/180;
handle = @(h0) test2fun(h0, theta, Bo, X);
Hopt_new(j2) = fsolve(handle, h0_new);
h0_new = Hopt_new(j2);
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by