create multiple arrays or matrix from function input

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2018년 5월 9일

답변:

2018년 5월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by