change size[ 3 1] to [3 25]

5 ビュー (過去 30 日間)
Haiwen Sun
Haiwen Sun 2020 年 10 月 28 日
回答済み: Sourabh Kondapaka 2020 年 11 月 6 日
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 Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by