How to create a 3D matrix from subtracting 2D matrices (like creating 2D matrix from subtracting vectors)?

2 visualizaciones (últimos 30 días)
Hi,
I wish to extend the behavior of subtracting a row vector and a column vector to matrices. Let me provide an example: Suppose A and B are two n x n matrices. I want to obtain matrix C such that C(:,:,i) = A(:,i)-B(i,:), i runs from 1 to n.
Is there a way to do this without for-loops?
Thanks,
Mohit.

Respuesta aceptada

Stephen23
Stephen23 el 19 de En. de 2022
C = permute(A,[1,3,2]) - permute(B,[3,2,1])
  2 comentarios
Stephen23
Stephen23 el 19 de En. de 2022
format compact
A = randi(9,3,3)
A = 3×3
5 9 5 1 5 9 5 9 6
B = randi(9,3,3)
B = 3×3
6 8 6 8 7 3 5 9 5
C = permute(A,[1,3,2]) - permute(B,[3,2,1])
C =
C(:,:,1) = -1 -3 -1 -5 -7 -5 -1 -3 -1 C(:,:,2) = 1 2 6 -3 -2 2 1 2 6 C(:,:,3) = 0 -4 0 4 0 4 1 -3 1

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by