How can i achieve this "linspace" problem?

9 views (last 30 days)
I should create 3 matrix (named as "d") with using linspace, but i can not use linspace command in for loop as at the code box. What is the reason of this problem?
Thanks,
kk=[8 10 12]
for j=1:numel(kk)
d(j)=linspace(0,50,kk(j))
end
alternatively;
for r=1:1:numel(kk)
for u=1:1:numel(kk)
dd(r,:)=linspace(0,50,kk(u))
end
end
I want to create 3 dd matrixes with using linspace by 8,10 and 12 values.

Accepted Answer

Stephen23
Stephen23 on 4 May 2018
Edited: Stephen23 on 4 May 2018
It is not possible to put multiple elements into one element of a numeric array. Use a cell array:
vec = [8,10,12];
d = cell(size(vec));
for k = 1:numel(vec)
d{k} = linspace(0,50,vec(k));
end
giving:
>> d{:}
ans =
0.00000 7.14286 14.28571 21.42857 28.57143 35.71429 42.85714 50.00000
ans =
0.00000 5.55556 11.11111 16.66667 22.22222 27.77778 33.33333 38.88889 44.44444 50.00000
ans =
0.00000 4.54545 9.09091 13.63636 18.18182 22.72727 27.27273 31.81818 36.36364 40.90909 45.45455 50.00000

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!