change size[ 3 1] to [3 25]

1 次查看(过去 30 天)
Haiwen Sun
Haiwen Sun 2020-10-28
a = [1 4 -2];
strColors = {'-r', '-g', '-b'};
for k=1:length(a)
LabFc3 = @(x)sin((a(k)/2)*pi*x).*(a(k)*x.^2+3);
fplot(LabFc3,[-8 8],strColors{k});
funcOut(k,:) = LabFc3(a(k)); linspace(-8,randi([0,8],1,1),25);
hold on;
end
This is my code here. In my instruction, it only gives me [1 4 -2] these three values. Everything is fine besides the funcOut size is [3 1], it requires [3 25]. How could I fix this?

回答(1 个)

Sourabh Kondapaka
Sourabh Kondapaka 2020-11-6
If we pre-allocate funcOut matrix of size 3 x 25, we can get the output you are trying to achieve. This is happening because data is being over-written.
Consider the following code:
a = [1 4 -2];
strColors = {'-r', '-g', '-b'};
% Pre allocating a matrix of size 3x25
funcOut = zeros(3,25);
for k=1:length(a)
LabFc3 = @(x)sin((a(k)/2)*pi*x).*(a(k)*x.^2+3);
fplot(LabFc3,[-8 8],strColors{k});
funcOut(k,:) = LabFc3(a(k)); linspace(-8,randi([0,8],1,1),25);
hold on;
end

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by