change size[ 3 1] to [3 25]

3 Ansichten (letzte 30 Tage)
Haiwen Sun
Haiwen Sun am 28 Okt. 2020
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?

Antworten (1)

Sourabh Kondapaka
Sourabh Kondapaka am 6 Nov. 2020
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

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by