How to delete a row if it is smaller than other rows with specific rule?

we say vector a <= b iff a(i)<=b(i) for i=1:size(a).
Now I need to compare rows of a matrix with that rule and delete the biggest row. of course, some rows are incomparable and I need to keep them as well. How can I do this in Matlab? For example for this Matrix
A=[59 98 30 171;
38 56 10 100;
72 100 29 149;
56 96 23 157;
58 97 22 260];
I would be very thankful if you help me. thank you.

11 Kommentare

Image Analyst
Image Analyst am 29 Jul. 2018
Bearbeitet: Image Analyst am 29 Jul. 2018
Please give your expected results for vectors little "a" and little "b".
And say how big 2-D matrix "A" is related to your condition involving 1-D vectors "a" and "b" on the first line.
And what exactly makes incomparable rows of 2-D matrix "A"? What would prevent you from comparing the two rows by subtracting them? It seems like every row should be able to be compared to any other row by subtraction.
"a" and "b" can be rows of matrix A. for example, we can delete the first rows of Matrix A because it is bigger than the sound row. row [38 56 10 100] is smaller than [59 98 30 171] so I should keep the small ones and delete bigger ones. I should compare all rows together.
[38 56 10 100] is smaller than [59 98 30 171] since 38<=59 & 56<=98 & 10<=30 & 100<=171.
Image Analyst
Image Analyst am 29 Jul. 2018
Bearbeitet: Image Analyst am 29 Jul. 2018
What if row 8 has all elements bigger than row 7 but not all bigger than row 9? Exactly which rows get compared? All against all? Just the above one vs. the one immediately below it? or above it?
Again, please give your expected output.
Ali Moghanni
Ali Moghanni am 29 Jul. 2018
Bearbeitet: Ali Moghanni am 29 Jul. 2018
Yes, All against all, and maybe the rows are not comparable. in that case, we do not delete. we want to delete comparable rows that follow the rules. for that example, the result after comparing is this. B=[38 56 10 100] I am expecting to have non-comparable rows at the end. Thank for your quick response.
Again what does not comparable mean to you?
Any rows can be compared - you just subtract them. What could prevent you from subtracting (comparing) them? After the subtraction, you use the all() function.
Have you seen the all() function?
I want to use my rule to compare the rows. for example [12 45 35 122] is not comparable with [15 40 35 120]. so If I have both these lines in my matrix I keep both rows. I did not see all() function. thanks
Watch while I compare those two rows:
comparison = [12 45 35 122] - [15 40 35 120]
There. They have been compared, if subtraction is what you mean by comparison.
Look again in the help for all(). It's been there forever.
No, subtraction is not my willing. comparison is defined is this way: we say vector a <= b iff a(i)<=b(i) for i=1:size(a). this is my comprison.
So all rows CAN be compared, like I said, and the comparison will be either true or false depending on whether all elements of row 1 are less than or equal to all elements of row 2, just like I did in my code below. Anything wrong with that code?
Thank you for the code. But I did not understand from the allGreaterThan, how you decide which rows should I keep and which rows should be deleted?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

See if this is what you want:
A=[59 98 30 171;
38 56 10 100;
72 100 29 149;
56 96 23 157;
58 97 22 260]
[rows, columns] = size(A);
allGreaterThan = false(rows, rows);
for row1 = 1 : rows
for row2 = 1 : rows
if all(A(row1,:) <= A(row2, :))
allGreaterThan(row1, row2) = true;
end
end
end
allGreaterThan
If compares every element of row1 to every element of rows and if all elements in row1 are less than or equal to the corresponding element in row 2, it sets allGreaterThan to true:
allGreaterThan =
5×5 logical array
1 0 0 0 0
1 1 1 1 1
0 0 1 0 0
1 0 0 1 0
0 0 0 0 1

6 Kommentare

Ali said: "Thank you for the code. But I did not understand from the allGreaterThan, how you decide which rows should I keep and which rows should be deleted?"
---------------------------------------------------------------------------
Well, what defines the "biggest" row?
  1. The one with the greatest difference between any of the elements with the corresponding element?
  2. The one with the greatest mean difference (over all elements) between it and the mean difference of each of the others?
  3. The one with the greatest mean value?
How do you think it should be done? Can you explain it in words?
This isn't homework is it? What's the use case?
of course, it is not homework. First of all, I am trying to compare 4 numbers (a vector a=[a(1) a(2) a(3) a(4)]) with other 4 numbers (a vector b=[b(1) b(2) b(3) b(4)]). I say vector 'a' is comparable to 'b' if we can compare all a(i) with b(i). I say 'a' is smaller than 'b' if a(i)<=b(i). So, I want to keep the uncomparable vectors. I compare all rows with all rows and delete big ones. I understand your code but I could not figure out how can I use allGreaterThan to get my Goal.
If you look at
allGreaterThan =
1 0 0 0 0
1 1 1 1 1
0 0 1 0 0
1 0 0 1 0
0 0 0 0 1
You'll see row 2 is smaller than all other rows. Row 4 is smaller than row 1 (indicated by the 1 in column 1), and not smaller than the other rows. So I guess you want to remove row 1. But do you want to remove row 4? It's smaller than one row and larger than the others, so do you remove it or not?
And to get rid of the diagonal 1's you should use < rather than <=.
Ali Moghanni
Ali Moghanni am 30 Jul. 2018
Bearbeitet: Ali Moghanni am 30 Jul. 2018
Yes, you got the idea, when I used < rather than <= it gives what I wanted. Now this example shows that I should delete rows 1 and 3 and 4. The ramaining rows 2 and 5 is the result. Now I understand that I should keep rows that they have all zeroes in the allGreaterThan column.
Why does row 3 get deleted? It has at least one element that is greater than the corresponding element in every other row. So it should not be deleted, right? You said it had to be less than every single other element, not just some of them.
My Matrix is this:
A =[59 98 30 171; 38 56 10 100; 72 100 29 149; 56 96 23 157; 58 97 22 90]; allGreaterThan =
5×5 logical array
0 0 0 0 0
1 0 1 1 0
0 0 0 0 0
1 0 0 0 0
1 0 1 0 0
first, row 1 > row 5, then I delete row 1. second, row 3 > row 5, then I delete row 3. third, row 4 > row 2, then I delete row 4. in the end, just row 2 and row 5 remains and I can say I can not delete one of them so I keep both.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by