How to find vectors that are multiple of other vectors inside a matrix

9 visualizzazioni (ultimi 30 giorni)
For example, given following matrix:
F = [0 1 2; 1 2 3; 0 2 4; 5 6 8; 2 4 6];
0 1 2
1 2 3
0 2 4
5 6 8
2 4 6
Wrong:Identify the rows that are (integer) multiples of some previous rows in the matrix.
In this case 3rd row and 5th row are multiple respectively of 1st and 2nd rows.
  • I want to copy the identified rows and create a new matrix out of it.
  • Delete the identified rows from the original matrix.
Edit:
Correct: The matrix is composed by integers, each element correspond to a part in weight of a substance so what matters is the proportion, the objective is to delete from the original matrix all the rows that have same proportion of elements. Doesn't matter rows order.
Inside the matrix we could find N rows that are multiple of others, I have to eliminate all of them except one the one that is multiple of 1.
So if I have two rows like this (I made them consecutive to simplify):
2 4 6
1 2 3
I should eliminate one of them, better in this case to delete first row so I can keep the row which is multiplied by one.

Risposta accettata

John D'Errico
John D'Errico il 25 Apr 2018
Modificato: John D'Errico il 25 Apr 2018
This is way easier than it has been made to be.
F = [0 1 2; 1 2 3; 0 2 4; 5 6 8; 2 4 6];
Assuming you have a recent release of MATLAB, do this:
Fscale = F./max(abs(F),[],2)
Fscale =
0 0.5 1
0.333333333333333 0.666666666666667 1
0 0.5 1
0.625 0.75 1
0.333333333333333 0.666666666666667 1
That scales the elements of F such that the MAXIMUM absolute element in any row is 1.
An older MATLAB release would have you write it as:
Fscale = bsxfun(@rdivide,F,max(abs(F),[],2));
Now, just look for replicate rows. uniquetol will suffice to solve the problem now.
[UniqF,I,J] = uniquetol(Fscale,10*eps,'byrows',true)
UniqF =
0 0.5 1
0.333333333333333 0.666666666666667 1
0.625 0.75 1
I =
1
2
4
J =
1
2
1
3
2
So these rows of F were scaled versions of some other row:
Frep = F;
Frep(I,:) = []
Frep =
0 2 4
2 4 6
We can look at the vector J to see that rows 1 and 3, and rows 2 and 5 appear twice. Those rows were multiples of each other. I suppose you could go further, and verify if the multiplier was an INTEGER factor easily enough. I don't know your real goal in this. Is it homework? If so, then they might slip in a row that is a factor of 2.5 times another, just to test your code.
If you want to discard all rows with a multiplier greater than 1? We can find ways to do that too.
  7 Commenti
Marco Salvo Cottone
Marco Salvo Cottone il 25 Apr 2018
Yes I thought it was faster too, but works.
Well your consolidator seems to be the killer choice for this kind of operation.

Accedi per commentare.

Più risposte (2)

Pawel Jastrzebski
Pawel Jastrzebski il 25 Apr 2018
Modificato: Pawel Jastrzebski il 25 Apr 2018
I think this will get you going. This code will help you identify which 'NextRow' is a multiplier of 'previousRow':
% DATA
F = [0 1 2; 1 2 3; 0 2 4; 5 6 8; 2 4 6]
% creating a matrix of conditions to be checked
% condition: is next row a multiplier of previous
nR = 2:size(F,1);
pR = 1:size(F,1)-1;
c1 = repelem(nR,pR)';
c2 = [];
for i=1:numel(pR)
ctemp = 1:i;
c2 = [c2 ctemp];
end
% 'c' holds all the cases that needs to be checked
c = [c1 c2'];
% clean the mess
clear c1 c2 ctemp i nR pR;
% 'R' results - true if next row is multiplier of a previous
R = sum(mod(F(c(:,1),:),F(c(:,2),:)),2) == 0;
% store conditions and the result in the table for convenience
t = table(c(:,1),c(:,2), R);
t.Properties.VariableNames= {'nextRow', 'previousRow','IsNextMultiOfPrevious'}
% clean the mess
clear c R;
The outcome:
t =
10×3 table
nextRow previousRow IsNextMultiOfPrevious
_______ ___________ _____________________
2 1 false
3 1 true
3 2 false
4 1 false
4 2 false
4 3 false
5 1 false
5 2 true
5 3 false
5 4 false
  6 Commenti
Marco Salvo Cottone
Marco Salvo Cottone il 25 Apr 2018
Modificato: Marco Salvo Cottone il 25 Apr 2018
Andrei Bobrov made a good point, the details I wrote were incorrect, and not true to the original question: "How to find vectors that are multiple of other vectors inside a matrix".
I'm correcting the question and adding details.
Jan
Jan il 25 Apr 2018
The clearing does not have benefits in Matlab, except for some huge arrays, which would exhaust the memory. Usually clearing variables reduces the processing speed only.

Accedi per commentare.


Andrei Bobrov
Andrei Bobrov il 25 Apr 2018
Modificato: Andrei Bobrov il 25 Apr 2018
n = size(F,1);
[x,~] = ndgrid(1:n);
l = tril(x,-1);
u = tril(x',-1);
ii = [u(u~=0),l(l~=0)];
R = arrayfun(@(x)rank([F(ii(x,1),:);F(ii(x,2),:)]),1:size(ii,1))' == 1;
out = table(ii,R,'v',{'rows','rank'});
corrected
s = size(F);
F1 = sortrows(F,-(1:s(2)));
[x,~] = ndgrid(1:s(1));
l = tril(x,-1);
u = tril(x',-1);
ii = [u(u~=0),l(l~=0)];
R = arrayfun(@(x)rank([F1(ii(x,1),:);F1(ii(x,2),:)]),1:size(ii,1))' == 1;
F_out = F1(~ismember((1:n)',ii(R,2)),:)

Community Treasure Hunt

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

Start Hunting!

Translated by