Can I change a 3-tensor into a matrix by indexing with a matrix?
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Leif Jensen
el 14 de Abr. de 2021
Comentada: Leif Jensen
el 14 de Abr. de 2021
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 comentarios
Respuesta aceptada
John D'Errico
el 14 de Abr. de 2021
Editada: John D'Errico
el 14 de Abr. de 2021
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?
Más respuestas (0)
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!