フィルターのクリア

subtraction ele by ele

1 回表示 (過去 30 日間)
Offroad Jeep
Offroad Jeep 2015 年 4 月 5 日
コメント済み: Image Analyst 2015 年 4 月 6 日
hi all,
consider any two matrix A and B of 3X3 . i want to subtract every element of A from each element of B one by one. kindly tell the command please.
  1 件のコメント
Stephen23
Stephen23 2015 年 4 月 6 日
MATLAB allows you to do this all at once using vectorized code, rather than one-by-one. This is faster, neater, and much less buggy code!
>> A = rand(3);
>> B = rand(3);
>> A - B
ans =
-0.15016 -0.043791 0.13661
0.74818 0.14698 0.12512
-0.84361 -0.70274 0.041771

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

採用された回答

Image Analyst
Image Analyst 2015 年 4 月 5 日
Not sure what you mean so I'll offer the two possibilities:
result = A - B;
or
A = randi(100, 3, 3)
B = randi([1,9], 3, 3)
aVector = A(:)
bVector = B(:)
loopCounter = 1;
for k1 = 1 : length(aVector);
for k2 = 1 : length(bVector);
result(loopCounter) = aVector(k1) - bVector(k2);
loopCounter = loopCounter + 1;
end
end
% Print to command window
result
  2 件のコメント
Offroad Jeep
Offroad Jeep 2015 年 4 月 6 日
wonderful................
Image Analyst
Image Analyst 2015 年 4 月 6 日
So, was it the second chunk of code (which is completely different than doing A-B) that did what you want?

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

その他の回答 (1 件)

David Young
David Young 2015 年 4 月 5 日
  1 件のコメント
Star Strider
Star Strider 2015 年 4 月 5 日
@4 X 4 —
David Young’s answer isn’t blank but maximally laconic. It is the ‘minus’ sign that will work for equally-sized matrices.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by