How can i achieve this "linspace" problem?

7 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2018-5-4
编辑:Stephen23 2018-5-4
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by