replace the b-th row and >30-th column of a by a-30

조회 수: 1 (최근 30일)
Chaoyang Jiang
Chaoyang Jiang 2018년 4월 1일
편집: Walter Roberson 2018년 4월 1일
I want to replace the b-th row and >30-th column of a by a-30. How to make the following code more efficient within one line? Thank you!
a=randi(100,[100,100]);
b=randi(100,[30,1]);
for i=1:length(b)
a(b(i),30:end)=a(b(i),30:end)-30;
end

채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 1일
There is no more efficient version for that.
I am not saying that it cannot be done in vectorized form: it can be done in vectorized form. I am saying that you need to make extra calculations and involve extra memory in such a way that the for loop is likely to be more efficient. Especially since duplicate b values are possible so you are going to be affecting the same location multiple times.
  댓글 수: 1
Chaoyang Jiang
Chaoyang Jiang 2018년 4월 1일
편집: Walter Roberson 2018년 4월 1일
Correct. Thank you for your answer.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by