Vectorization using expressions with matrices

I am trying to vectorize my Matlab code and I am not being able to do that because when I use a value of matrix inside an expression I cannot do that element-by-element. Ok... that was a little bit confusing I will try to explain with some code lines.
Imagine I have to calculate some values using the pre-calculated values contained in a matrix, let's say A. E.g.:
for i=1:100;
for j=1:100;
value((i-1)*100+j)=constant*A(i,j);
end
end
This would take a long time, since it is using two for's.
If I try to vectorize this, I would do so:
i=1:100;
j=1:100;
value=constant*A(i,j);
Expecting that the output variable, value, would be a vector. But (you can try yourself), the output will be a [ i x j ] matrix, since the operational logic will not be element-by-element but the combination of the i and j vectores.
Hope that I was unambiguous and that someone can help me.
Thanks a lot in advance!
Kind regards,

回答 (1 件)

Sean de Wolski
Sean de Wolski 2012 年 2 月 14 日

2 投票

value2 = constant*(reshape(A',numel(A),1)); %transpose, reshape, multiple
isequal(value,value2)

1 件のコメント

Claudio
Claudio 2012 年 2 月 24 日
Thank you very much!!
Solved my problem :)
Thanks again!

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

質問済み:

2012 年 2 月 14 日

編集済み:

2013 年 10 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by