フィルターのクリア

how to perform the double summation for this equation?

2 ビュー (過去 30 日間)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016 年 4 月 15 日
回答済み: Walter Roberson 2016 年 4 月 15 日
if i have (n,m) binary Matrix M and and i want to generate a random (n,m) matrix A and then apply this

採用された回答

Walter Roberson
Walter Roberson 2016 年 4 月 15 日
sum(sum(m ~= a)))
You said it was a binary matrix, so the only values are 0 and 1. If the two values are the same, both 0 or both 1, then the difference is 0 and abs(0) is 0. If m is 1 and a is 0 then the difference is 1 and abs(1) is 1. If m is 0 and a is 1, then you need to know what you mean by subtraction. If you were modeling by uint8 then uint8(0) - uint8(1) would be 0 because uint8 underflows at 0, not able to represent negative values. But then it would make no sense to have the absolute value bars. And perhaps those absolute value bars are magnitude bars, so the calculation should be double(m) - double(a), then 0 - 1 would give -1 and abs(-1) would be 1. So you want 0 if the two values are the same, and you want 1 for [1 0] and 1 for [0 1], so you want 1 if the two values are different. And that is what you get if you compare the two using ~= . You do not need an subtraction or abs(), the ~= operator gives you exactly the value you want.

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 15 日
n=3
m=4
M=randi([0 1],n,m)
A=rand(n,m)
[ii,jj]=meshgrid(1:n,1:m)
out=sum(abs(A(ii(:))-abs(M(jj(:)))))

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by