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

2 ビュー (過去 30 日間)
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.

採用された回答

Stephen23
Stephen23 2022 年 1 月 19 日
C = permute(A,[1,3,2]) - permute(B,[3,2,1])
  2 件のコメント
Stephen23
Stephen23 2022 年 1 月 19 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFilter Analysis についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by