Subtracting a value in a 3D array with all values in another 3D array

8 ビュー (過去 30 日間)
Sarah Bassiouny
Sarah Bassiouny 2022 年 11 月 26 日
コメント済み: Jan 2022 年 11 月 27 日
Hello, I have 2 three-dimensional arrays, i would like to take the first value in the first array and subtract it with all values in the second array, and then the second value in the first array with all values in the second array, and in each time the smallest value of the subtraction is put in a new array.
Thank you in advance
  2 件のコメント
Matt J
Matt J 2022 年 11 月 26 日
編集済み: Matt J 2022 年 11 月 26 日
What are the sizes of the arrays?
Does "smallest" mean min(x-y) or min(abs(x-y))?
Sarah Bassiouny
Sarah Bassiouny 2022 年 11 月 26 日
min(abs(x-y))

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

採用された回答

Jan
Jan 2022 年 11 月 26 日
編集済み: Jan 2022 年 11 月 26 日
A = rand(5,6,7);
B = rand(6,7,8);
D = min(A(:).' - B(:), [], 1);
Result = reshape(D, size(A));
If the intermediate matrix A(:).'-B(:) exhausts the available RAM, use a loop:
Result = zeros(size(A));
for k = 1:numel(A)
Result(k) = min(A(k) - B, [], 'all');
end
  2 件のコメント
Sarah Bassiouny
Sarah Bassiouny 2022 年 11 月 26 日
okay perfect, i got the results of the smallest value, now i need to know the position of that value and display it in the form of an array, so if the smallest value is in position 4 then the value in the results matrix is the difference, and the array is 6.
Thank you in advance
Jan
Jan 2022 年 11 月 27 日
[D, Pos] = min(A(:).' - B(:), [], 1);
determines the position also as linear index. But what do you exactly need as output "position" for 3D arrays?
I do not understand, what this means: "if the smallest value is in position 4 then the value in the results matrix is the difference, and the array is 6." Why 6?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by