Find the difference between all columns of a given row of a matrix
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MAWE
am 8 Mai 2023
Kommentiert: MAWE
am 8 Mai 2023
I have MXL matrix. For each row, I want to find the absolute difference between the columns, without repeating. For example, if I have a row
a = [a11 a12 a13 a14]
I want to find
[abs(a11-a12) abs(a11-a13) abs(a11-a14) abs(a12-a13) abs(a12-a14) abs(a13-a14)]
How can I do that withoug for loops?
EDIT: I found this method
pdist([2 3 1 4]',@(x,y) x-y)
which looks like what I'm looking for, but I need to extend this for a matrix, and perform the above for all rows.
0 Kommentare
Akzeptierte Antwort
Antoni Garcia-Herreros
am 8 Mai 2023
Hello MAWE,
Something like this might work for your case:
A=rand(4,5); % Generate data for the example
perms = nchoosek(1:size(A,2),2); % Find all the possible permutations
D=abs(A(:,perms(:,2))-A(:,perms(:,1))) % Compute the difference
Hope this helps
Weitere Antworten (1)
Luca Ferro
am 8 Mai 2023
Bearbeitet: Luca Ferro
am 8 Mai 2023
a=[1 3 5 -3];
aPerms=nchoosek(a,2); %generates all unique couples
absDiff=abs(aPerms(:,1)-aPerms(:,2))' %absolute difference of said couples, ' to transpose
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!