sum of a vector with non zero elements of matrix without using loop

1 回表示 (過去 30 日間)
summyia qamar
summyia qamar 2018 年 8 月 28 日
コメント済み: summyia qamar 2018 年 8 月 28 日
I have a matrix T and vector M say
T=[0 0 1 0 0 0 1 0 0 0
0 1 0 0 0 1 0 0 1 1
0 1 0 0 0 0 0 0 1 1
0 0 1 0 1 0 1 1 1 0
0 0 1 1 1 1 0 1 1 0]
M=[0.1 0.2 0.2 0.4 0.2 0.3 0.1 0.1 0.1 0.2]
I want to add M in each row of T with only non zero element. For example, the result of 1st row should be
0 0 1.2 0 0 0 1.1 0 0 0
but is this possible without applying for loop?
  3 件のコメント
Image Analyst
Image Analyst 2018 年 8 月 28 日
How are you getting 1.1 and 1.2? what values are being summed to give those numbers? You say for "each row of T with only non zero element." Well, that would be every single row of T because every single row of T has non-zero elements. And M sums to 1.9, so wouldn't every element of the output matrix be 1.9? And give any remaining rows of the output - why did you give only the first row???
Even if M took on the values of T where the elements of T were not zero, that would give output(1,:)=[0 0 0.2 0 0 0 0.1 0, 0, 0] because the 3rd and 7th element of T in that row are non-zero. I have no idea what you want.
summyia qamar
summyia qamar 2018 年 8 月 28 日
I got the answer

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

採用された回答

Pierre845
Pierre845 2018 年 8 月 28 日
You can do:
R = repmat(M, 5, 1);
Q = (T+R).*T;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by