bsxfun - @minus - how to divide by maximum

Hello everyone,
I have two arrays:
a = [1; 2; 3]
b = [1; 2; 3; 4]
out = abs(bsxfun(@minus,a,b')./a)
I am getting this:
out = [0 1 2 3
0.5 0 0.5 1.0
0.67 0.33 0 0.33]
I am looking for a way to divide not simply by ./a, but to divide by the maximum number in the numerator. So instead of calculating abs(1-3)/1=2 for out(1,3) I would like the calculation to be abs((1-3)/3)=0.67.
Any help is greatly appreciated.

 Risposta accettata

Bruno Luong
Bruno Luong il 28 Dic 2020
Modificato: Bruno Luong il 28 Dic 2020
Do do poor job to explain, up to everyone guess
>> a = [3; 5; 1;]
a =
3
5
1
>> b = [3; 2; 3; 4]
b =
3
2
3
4
>> abs(a-b')./max(a,b')
ans =
0 0.3333 0 0.2500
0.4000 0.6000 0.4000 0.2000
0.6667 0.5000 0.6667 0.7500
>> abs(bsxfun(@minus,a,b'))./bsxfun(@max,a,b')
ans =
0 0.3333 0 0.2500
0.4000 0.6000 0.4000 0.2000
0.6667 0.5000 0.6667 0.7500

1 Commento

My bad, I just didnt have the apostrophe in max(a,b') after b and was wondering about dimension problems in my original data. Thanks.

Accedi per commentare.

Più risposte (1)

Ko - if you want to divide by the maximum of a, couldn't you just do
out = abs(bsxfun(@minus,a,b')./max(a))
?

3 Commenti

Thanks for your answer, I am actually not just looking to divide by the maximum of a, but to b, if that number happens to be bigger. Your solution works with the example I have provided, so let me try and clarify by another example
a = [3; 5; 1;]
b = [3; 2; 3; 4]
Your Solution gives out this:
out = abs(bsxfun(@minus,a,b')./max(a))
out = [0 0.2 0 0.2
0.4 0.6 0.4 0.2
0.4 0.2 0.4 0.6]
What I would want the output to look like is this:
out = [0 0.33 0 0.25
0.4 0.6 0.4 0.2
0.67 0.5 0.67 0.75]
An examplary calculation for the upper right term would be: (3-4)/4
I hope this clarifys it, thanks for your time
But max of a is 5. So where does the 4 come from??
Also, you don't need bsxfun.
abs((a-b')/max(a))
4 is the maximum numerator, so the number I want to divide with could be from either a or b. whatever the maximum numerator is. hope that helps
Thanks, I'll be taking bsxfun away then.

Accedi per commentare.

Categorie

Scopri di più su Mathematics in Centro assistenza e File Exchange

Tag

Richiesto:

il 28 Dic 2020

Commentato:

il 28 Dic 2020

Community Treasure Hunt

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

Start Hunting!

Translated by