필터 지우기
필터 지우기

Replace arrays of a matrix with a vector

조회 수: 1 (최근 30일)
amin
amin 2020년 2월 4일
댓글: amin 2020년 2월 4일
Hi,
I have a matrice R like :
R=zeros(3);
I need to change 3 array of R:
c=[1 1;3 2;1 2];
with new values in this vector:
Newvalue=[ 3 7 9];
So the final result is :
R=[3 9 0;0 0 0;0 7 0]
I know that I can do it easily with a For loop to check every element and replace new value.
But I want to vectorize this, i.e. with no loop.

채택된 답변

KSSV
KSSV 2020년 2월 4일
편집: KSSV 2020년 2월 4일
Read about sub2ind.
R=zeros(3);
c=[1 1;3 2;1 2];
Newvalue=[ 3 7 9];
% get global indices
idx = sub2ind(size(R),c(:,1),c(:,2)) ;
R(idx) = Newvalue
  댓글 수: 1
amin
amin 2020년 2월 4일
Thank you so much, it is exacly what I was looking for.

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

추가 답변 (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