Pairwise subtraction of rows in the same matrix

Hello guys...
Please I have this code
How can I subtract each row from all other rows(R) ... like subtracting R1-R1; R1-R2, R1-R3... R1-Rn+1 AND
then R2-R1, R2-R3.... R2-Rn+1 for all the rows
A1 = [...
0 0 1 1 1
0 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
0 0 1 1 1
1 0 1 0 1
0 0 0 0 1
1 1 1 1 1
1 1 1 1 1
0 1 1 1 1
1 0 1 0 1
0 0 1 0 1
1 1 1 1 1
1 1 1 1 1
1 0 1 1 1
0 0 1 1 1
0 1 1 1 1
1 1 1 1 1];
I need this urgently....
Thanks

6 Commenti

Is the expected result one matrix of the same size as A1 ?
Hi Adedeji,
In the more recent versions of Matlab the expression
A - A(:,n)
conveniently subtracts the nth column of A from each and every column of A.
[comment intended to provide an expression that could be used as a building block]
Thanks David,
However, this does not work.
Each row stands for a customer with different 5 properties (The columns).
I wish to perform a pairwise subtraction all customers from the others such that all permuations have the results of the 5 attributes each.
Why do you need this quirky thing? Is it homework?
Have you tried diff()?
It's not a homework but part of an ongoing project of mine
as I explained while replying to David's answer.
I wish to subtract each customers 5 attributes (The Columns) from the other customers'.
The rows are the customers
The columns are their 5 attributes.
I have tried Diff()
but it's not giving me a pairwise subtraction across all possible combinations of rows there could be.

Accedi per commentare.

 Risposta accettata

madhan ravi
madhan ravi il 12 Mag 2019
Modificato: madhan ravi il 12 Mag 2019
Aa= reshape(a',1,size(a,2),[])- a; % a your matrix , each page of Aa represents each row’s subtraction
A=reshape(permute(Aa,[2,1,3]),size(a,2),[]).'

7 Commenti

Thanks all,
However, non of these work.
Each row stands for a customer with different 5 properties (The columns).
I wish to perform a pairwise subtraction all customers from the others such that all permuations have the results of the 5 attributes each.
First you said rows now your saying columns?? Illustrate with a short 2 by 2 matrix and show how your output should look like.
My initial question actually said rows if you check. I was jsut being explicit with what they actually stand for.
FOr example, given
D =
2 3 4 5 6
4 5 2 6 7
3 5 7 3 7
I want to subtract Row1-Row1, Row1-ROw 2, Row1-Row3
Row2-Row1, Row2-Row2, Row2-Row3
Row 3-Row1, Row3-Row2, Row3-Row3
Then have them indexed such that I know which row subtraction is which result.
Thanks Ravi.....
That's what my code does:
Wanted = reshape(D',1,size(D,2),[]) - D
% Wanted(:,:,1) - represents subtraction of first row with all the other rows und so weiter
If the above doesn't do what you want then you need to show explicitly how your output should look like.
When I tried your code, it kept flagging the error below
Error using -
Matrix dimensions must agree.
Wanted = bsxfun(@minus,reshape(D.',1,size(D,2),[]), D) % the reason was your using version prior to 2016b, that's why when you ask a question you should fill up the field "Release" thereby these kinds of misunderstandings can be avoided
Exactly!!!!!!
This gives me what I want...
But I also want to sum all the elements for each row and divide it by 5 for all the possible combinations.
So I end up with a 3 by 3 matrix again.
Please how do you advice I do this.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by