Storing variables without overwriting them

Hello guys,
I made a piece of code that run through all the elements of each row and calculate the convolution. my problem is how can I store the previous answers without overwriting them
A=[1,2;3,4];
v=[1 -2 1];
y=[];
for i=1:length(A)
x=A(i,:);
for c=1:2
y=conv(v,x);
end
hold on
end
The result should be a matrix exp
result
1 0 -3 2
3 -2 -5 4
instead I only get the last row
3 -2 -5 4

 採用された回答

the cyclist
the cyclist 2017 年 1 月 14 日
編集済み: the cyclist 2017 年 1 月 14 日

1 投票

Do this instead
y(i,:)=conv(v,x);
You should probably also preallocate the memory for y before the loop, like
y = zeros(2,4)
or however you would determine the final dimensions. "Growing" the array row-by-row is poor memory management in MATLAB, and can lead to significantly slower execution time for large arrays.
Also, I am not sure what you expect the "hold on" command to be doing there. That would only be related to plotting.

1 件のコメント

Massi Friha
Massi Friha 2017 年 1 月 14 日
Thank you very much sir :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2017 年 1 月 14 日

編集済み:

2017 年 1 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by