How to put the values into the specific location?

조회 수: 23 (최근 30일)
Isti
Isti 2012년 5월 3일
I have a list of location like this:
location =
1 2
1 4
2 3
4 2
From above, it describes thatt the location is (1,2),(1,4),(2,3),and (4,2)
Then i have a values like this:
Values =
55
22
99
11
I want to put the values into the location above. So the result of matrix A is like this:
A =
2 55 4 22
4 7 99 5
9 1 2 5
7 11 4 2
What to do?
Thanks before :')

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 5월 3일
A = zeros(4);
location =[...
1 2
1 4
2 3
4 2];
Values =[...
55
22
99
11];
A(sub2ind(size(A),location(:,1),location(:,2))) = Values;
OR:
A(bsxfun(@minus,location,[0 1])*[1;size(A,1)]) = Values;
  댓글 수: 2
Isti
Isti 2012년 5월 3일
thanks :)
anyway what's the difference between sub2ind and bsxfun function?
Andrei Bobrov
Andrei Bobrov 2012년 5월 3일
please read about functions:
sub2ind - http://www.mathworks.com/help/techdoc/ref/sub2ind.html;
bsxfun - http://www.mathworks.com/help/techdoc/ref/bsxfun.html.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by