Populating a matrix with predetermined values

조회 수: 2 (최근 30일)
Alice Stembridge
Alice Stembridge 2015년 5월 3일
댓글: Alice Stembridge 2015년 5월 14일
I have a N x N matrix which I have defined as X, I have another matrix that contains values I would like to populate my matrix X with.
For example, if X is a 3 x 3 matrix and A is the matrix below:
A =
3 15
4 16
5 17
6 18
7 19
How do I populate the matrix X with the values in the second column of A where the first column is the position in the matrix X. i.e I would like the final matrix X to be
X = 0 0 15
16 17 18
19 0 0
Thank you.

채택된 답변

James Tursa
James Tursa 2015년 5월 3일
X = zeros(N,N);
X(A(:,1)) = A(:,2);
X = X';
  댓글 수: 3
James Tursa
James Tursa 2015년 5월 4일
The example you gave works with linear indexing into a 2D matrix. For a 3D array you will have to give a short example of where the elements would go before we can answer how to do it and whether linear indexing will work.
Alice Stembridge
Alice Stembridge 2015년 5월 14일
Hi James, Sorry I did not see that you had commented again. I think I have figured it out now. Thank you though.

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

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