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

2 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Stephen23
Stephen23 il 19 Gen 2022
C = permute(A,[1,3,2]) - permute(B,[3,2,1])
  2 Commenti
Stephen23
Stephen23 il 19 Gen 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

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by