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

조회 수: 1 (최근 30일)
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Pierre845
Pierre845 2018년 8월 28일
You can do:
R = repmat(M, 5, 1);
Q = (T+R).*T;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by