Subtracting one element of an array from all elements of another array

8 ビュー (過去 30 日間)
Hamid
Hamid 2022 年 7 月 29 日
コメント済み: Hamid 2022 年 8 月 1 日
Dear all,
I have two (40*40) arrays and want to subtract every element of one array from all elements of the other one. Is there any function to avoild using loops? How can I use loops for that? Thank you in advance.
hit1=load ('hit1.mat');
hit2=load ('hit2.mat');

採用された回答

Walter Roberson
Walter Roberson 2022 年 7 月 29 日
編集済み: Walter Roberson 2022 年 7 月 29 日
result4d = hit1 - permute(hit2, [3 4 1 2]);
This will be 40 x 40 x 40 x 40.
Depending on what you are doing, you might also want to consider something like
result2d = pdist2(hit1(:), hit2(:))
and then reshape() if desired. Note that pdist2 would not be negative.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by