필터 지우기
필터 지우기

How to write matrix elements with its position?

조회 수: 6 (최근 30일)
Ammy
Ammy 2018년 1월 25일
댓글: Ammy 2018년 1월 25일
I have a matrix A=[1 2 3; 4 5 6; 7 8 9] I want to write each entry with its row and column position e.g 111 indicates the element 1 at position first row First column , also 328 element 8 at position 3rd row and second column. so with the help of above matrix
A I want the following
111
122
133
214
225
236
317
328
339
Thanking for the anticipation.
  댓글 수: 1
Guillaume
Guillaume 2018년 1월 25일
While it's very easy to do what you want, what is its purpose? Bearing in mind that if you don't limit A to integers [0-9] and its size to a max of 9 rows and 9 columns, then the operation is irreversible.
What would the value 1234 mean?
  • 1st row, 2nd column, value 34?
  • 12th row, 3rd column, value 4?
  • 1st row, 23rd column, value 4?

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

채택된 답변

Birdman
Birdman 2018년 1월 25일
편집: Birdman 2018년 1월 25일
[r,c]=find(A);
val=sortrows([r c A(:)],1)
  댓글 수: 2
Guillaume
Guillaume 2018년 1월 25일
That will fail if any value in A is 0.
[r, c] = find(true(size(A));
val = sortrows([r c A(:)],1)
would be safer.
Or:
[c, r] = ndgrid(1:size(A, 2), 1:size(A, 1));
val = [r(:), c(:), a(:)];
Birdman
Birdman 2018년 1월 25일
Thanks.

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

추가 답변 (2개)

Domanic
Domanic 2018년 1월 25일
Or ind2sub:
A = [1 2 3; 4 5 6; 7 8 9].';
[a,b] = ind2sub(size(A),1:numel(A));
fprintf('%d%d%d\n',[b.' a.' A(:)].')

Jan
Jan 2018년 1월 25일
What about loops?
A = [1 2 3; 4 5 6; 7 8 9];
[s1, s2] = size(A);
for i1 = 1:s1
for i2 = 1:s2
fprintf('%d%d%g\n', i1, i2, A(i1, i2));
end
end
  댓글 수: 1
Ammy
Ammy 2018년 1월 25일
Thank you very much, this code really work

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by