필터 지우기
필터 지우기

how create a new matrix with values found in an another matrix

조회 수: 3 (최근 30일)
gianluca
gianluca 2012년 9월 22일
Hi, I've the matrix A with dimension (m*n*p). I've found the indexes of the first non-zero elements along the dimension p. They are stored in I with dimension m*n. I would create a new matrix B with dimension m*n with the values corresponding to the indexes I taken in A.
Thanks, Gianluca
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 22일
what if you have'nt n*m non zeros elments, can you explain?
gianluca
gianluca 2012년 9월 22일
hi, the matrix A has all zeros except m * n values scattered in the p dimension. The matrix I say me for each row and column (size is m*n) in what page there is the first cell with value different by zero. I would create a new matrix with those values.

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

채택된 답변

Sven
Sven 2012년 9월 23일
편집: Sven 2012년 9월 23일
Hi Gianluca,
Have you tried using the second output from find_ndim?
[I, Imap] = find_ndim(A,3,'first');
B = A(Imap); % The actual elements of A that were "found" above, as a vector
B = reshape(B, size(I)); % size(I) is just [m n] from your question
Basically, the elements of the variable I (output from find_ndim) are the subscript references of the 3rd dimension of A. You can also use sub2ind() to get the same result as above, you just also need the first and second subscript references:
[subs2ndDim,subsFirstDim] = meshgrid(1:size(A,2),1:size(A,1));
inds = sub2ind(size(A), subsFirstDim, subs2ndDim, I)
B = A(inds);
Hope this helped you out.

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