How to store vectors with different size in a loop ?

18 ビュー (過去 30 日間)
Ole
Ole 2016 年 3 月 23 日
コメント済み: dpb 2019 年 4 月 15 日
How to store vectors with different size and how to access them. M(k) found_roots
F = @(t) cos(t)*exp(-t);
A = @(s) arrayfun(@(s)-integral(F,-10,s),s);
Z = @(t1,t2) -integral(A,t1,t2);
interval = [-5, 5];
N = 100;
start_pts = linspace(interval(1),interval(2),N);
found_roots = [];
for k = 1:10
t2 = k;
for i=1:numel(start_pts)-1
try
found_roots(end+1) = fzero(@(t1)Z(t1,t2),[start_pts(i),start_pts(i+1)]);
end
% i
end
M(k) = found_roots % vector of different length
k
end
  2 件のコメント
Benjamin
Benjamin 2019 年 4 月 15 日
But in every loop, if the size of the matrix (output here) changes Matlab gives error of different matrix sizes. Is there anysolution for that.
FYI: my Matlab code generates a matrix of [A] = n*2 and in every loop (for i = 1:10) n changes. I would like to store all A results in another B matrix.
dpb
dpb 2019 年 4 月 15 日
That's what the Answer shows...use cell array instead of "regular" array...the difference is "the curlies" the {} around the subscript, not ()

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

採用された回答

dpb
dpb 2016 年 3 月 23 日
Use cell array instead; the curly brackets...
M{k} = found_roots;
  2 件のコメント
Ole
Ole 2016 年 3 月 25 日
編集済み: Ole 2016 年 3 月 25 日
Thanks This works but now how to plot them .
dpb
dpb 2016 年 3 月 25 日
"them" can be plotted, sure, but against what?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by