Change matrix elements in a loop?

조회 수: 2 (최근 30일)
lsutiger1 . 2015년 11월 6일
댓글: Star Strider . 2015년 11월 6일
I am trying to figure out how to use a for loop to change the elements in a matrix.
I want to do this ten times.
For example, if row 1 of a 6x3 matrix is (a,a,0), I want to make a loop that generates row 2 as (2a,2a,0). I am basically trying to multiply the initial row to make all 6 rows increase by a multiple of a.
Thank you in advance for any suggestions!

채택된 답변

Star Strider
Star Strider 2015년 11월 6일
No loop necessary. Use bsxfun:
M = randi(9, 6, 3);
Mm = bsxfun(@times, M, [1:size(M,1)]')
This multiplies every row of ‘M’ by the column vector [1 2 ... 6]'.
  댓글 수: 8
lsutiger1 2015년 11월 6일
Yes, I changed it because I was getting that error; neither the 1 or 3 seems to make a difference. This is why I am confused.
Star Strider
Star Strider 2015년 11월 6일
I can’t run your code because I have no idea what you are doing and the inputs should be. Please attach a .mat file with the appropriate size and values for ‘type’. Use the ‘paperclip’ icon, and complete both the ‘Choose file’ and ‘Attach file’ steps.
My code does what you want it to, as you requested, but I can’t troubleshoot it with your code without the appropriate data.

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

추가 답변 (0개)


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

Community Treasure Hunt

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

Start Hunting!

Translated by