How do I delete the rows in amtrix wherre one column is less than another.

6 visualizaciones (últimos 30 días)
I have a matrix
[8 29 46 75;
8 29 58 50
8 29 58 75
8 46 58 75
29 46 58 50
29 46 58 75
8 29 46 25
8 46 58 25]
I need to find the rows where the fourth column is the highest value. For example 29 46 58 50
should be deleted and
29 46 58 75 should remain.
I have already tried unique but maybe I am not using enough qualifiers.
Also
MaxTripUnique = TripUnique((max(TripUnique(:,4))),:) Attempted to access TripUnique(75,:); index out of bounds because size(TripUnique)=[9,4].
with no luck.
Any help is appreciated as all the ideas on the internet don't seem to work.
This is part of a program where the matrix is created in a loop and I need to take the final values and concatenate them into the matrix that will be used for a further calcualtion.

Respuestas (2)

Joseph Cheng
Joseph Cheng el 22 de Mayo de 2015
Editada: Joseph Cheng el 22 de Mayo de 2015
can be simply performed by
testmat = [8 29 46 75;8 29 58 50;8 29 58 75;8 46 58 75;29 46 58 50;29 46 58 75;8 29 46 25;8 46 58 25];
[val ind]=max(testmat,[],2)
newmat = testmat(ind==4,:)
so by using the dimension parameter inside the max() function we can tell it to perform the max per row and tell us which index the max was detected. Then using the variable ind we can define which rows has the maximum value in the 4th column.
  3 comentarios
Stephen23
Stephen23 el 23 de Mayo de 2015
Editada: Stephen23 el 23 de Mayo de 2015
@Thomas Sisk: You are introducing a new requirement which seems to be related to pairs or rows, something that your original question does not explain. As amazing as it might seem, we can't read your mind: Joseph Cheng's answer resolves the original question that you have asked. If you have some different or new conditions that need to be fulfilled, then you need to explain them clearly, preferably with examples too.
And please take the time to format your code properly: it make sit much clearer for us to work with:
Joseph Cheng
Joseph Cheng el 3 de Jun. de 2015
Sorry, been too busy to check Answers. I do not understand the new requirement. Your initial request was clear but I do not understand the condition that you'd keep the second row? Just because it is larger of the two last rows is not enough as your initial condition wipes it out. (also in your question you have that row deleted as your example).

Iniciar sesión para comentar.


Brendan Hamm
Brendan Hamm el 22 de Mayo de 2015
Editada: Brendan Hamm el 2 de Jun. de 2015
To delete the rows of A where the fourth column is not the largest value in that row, we just need to perform a logical comparison. To do this we need a matrix containing the 4th column which has the same size as the first 3 columns.
cmpMat = repmat(A(:,4),1,3); % A 3 column vector with the last column repeated
isFour = A(:,1:3) > cmpMat; % Logical with 1 where value is larger than 4th column
Now we need to check if any of the values in a row are larger than 4th column and delete it.
rowIdx = any(isFour,2); % Logical identifying rows with a value larger than 4th col
A(rowIdx,:) = [];
Note: max is returning to you the maximum value and this is why this does not work as an index. The second output of max is the index, but this uses linear indexing if passed a matrix.
  2 comentarios
Thomas Sisk
Thomas Sisk el 22 de Mayo de 2015
TripUniqueTest =
8 29 46 75
8 29 58 50
8 29 58 75
8 46 58 75
29 46 58 50
29 46 58 75
8 29 46 25
29 46 58 25
>> cmpMat = repmat(TripUniqueTest(:,4),1,3)
cmpMat =
75 75 75
50 50 50
75 75 75
75 75 75
50 50 50
75 75 75
25 25 25
25 25 25
>> isFour = TripUniqueTest(:,1:3)>cmpMat
isFour =
0 0 0
0 0 1
0 0 0
0 0 0
0 0 1
0 0 0
0 1 1
1 1 1
>> rowIdx = any(isFour,2)
rowIdx =
0
1
0
0
1
0
1
1
>> TripUniqueTest(rowIdx) = []
TripUniqueTest =
Columns 1 through 11
8 8 8 29 29 29 29 46 46 46 29
Columns 12 through 22
46 46 58 58 58 58 58 46 58 75 50
Columns 23 through 28
75 75 50 75 25 25
The above is what I got using this method any ideas whats wrong.
Brendan Hamm
Brendan Hamm el 2 de Jun. de 2015
Sorry, that last line should read:
TripUniqueTest(rowIdx,:) = [];

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by