change size[ 3 1] to [3 25]

3 visualizaciones (últimos 30 días)
Haiwen Sun
Haiwen Sun el 28 de Oct. de 2020
Respondida: Sourabh Kondapaka el 6 de Nov. de 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?

Respuestas (1)

Sourabh Kondapaka
Sourabh Kondapaka el 6 de Nov. de 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

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by