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

回答(1 个)

Jos (10584)
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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by