create multiple arrays or matrix from function input
1 回表示 (過去 30 日間)
古いコメントを表示
I'm creating a function where the input 'h' is an array of numbers, and then trying to use a for loop to establish arrays with step sizes of each number listed in 'h'. for example, if the function is summoned as function([pi/2 pi/4 pi/8])
then the for loop ends up creating 3 specific arrays OR a 3X1 matrix where x1=[3*pi:pi/2:29*pi]; x2=[3*pi:pi/4:29*pi]; x3=[3*pi:pi/8:29*pi];
Is there anyway to do this? If 'h' ends up being 5 values long then I'd like it to return 5 individual arrays or a 5x1 matrix
0 件のコメント
回答 (1 件)
Jos (10584)
2018 年 5 月 9 日
Here is an example. I suggest you store the result in a cell array where each cell can hold a vector with a different length
X = my function(h)
N = numel(h) ;
X = cell(1,N) ; % pre-allocation
for k=1:N
% fill each cell with a vector
X{k} = 3*pi:h(k):29*pi ;
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!