Can I change a 3-tensor into a matrix by indexing with a matrix?
조회 수: 26 (최근 30일)
이전 댓글 표시
The general problem I have is this: I have a 3-tensor A of size MxNx4. My goal is to create a Matrix B, where B(i,j) = A(i,j,argmin(abs(B),[ ],3)), so Element (i,j) of B is the absolut minimum of the elements of A at location (i,j,:) multiplied with its sign. Currently my code uses a for loop, but I would like to implement this faster and witout the loop. Is this possible?
Current code:
[K,I] = min(abs(A),[],3);
B = zeros(M,N);
for i = 1:M
for j = 1:N
B(i,j) = A(i,j,I(i,j));
end
end
댓글 수: 0
채택된 답변
John D'Errico
2021년 4월 14일
편집: John D'Errico
2021년 4월 14일
A = randn(4,4,3) % Not very creative in making up numbers. So sue me. :)
[~,I] = min(abs(A),[],3)
What is I? For every combination of row and column in A, this is the plane containing the min abs value of A. So just grab the indicated element directly, rather than the absolute value of the element, and then attaching the sign. But this requires indexing using a single index.
B = A((1:4)' + 4*(0:3) + 16*(I-1))
I could have used ind2sub also, but why bother?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!