MATLAB Answers

Count the number of times a value appears in a row and save the row number

1 view (last 30 days)
I wanted to sort points in an array based on how many times a number less than or equal to 1.5 appears in the row. I want to save the row index number of those that have 3 or more occurences less than or equal to the target value of 1.5. I have placed the array below. If you have any ideas on how to do this that would be great. Thanks for your time.
A = [NaN 1.00000000000000 1.00000000000000 1.99994495218295 1.40991828545270 1.98465118243128 2.97995391814023 1.41286932041226 1.44150019470391 2.30703739193881
1.00000000000000 NaN 1.41618367600370 0.999999999999996 1.00000000000000 2.11349467948903 1.98589244648483 1.00000000000000 1.73544845449717 1.98611502337309
1.00000000000000 1.41618367600370 NaN 2.24512987514306 2.20570040562793 0.999999999999996 3.22884118121504 1.75480404405606 1 3.15259384660772
1.99994495218295 0.999999999999996 2.24512987514306 NaN 1.40867173007472 2.65578172203034 1.00000000000000 1.41514975278174 2.44463958996392 2.12446752335984
1.40991828545270 1.00000000000000 2.20570040562793 1.40867173007472 NaN 3.01897607767301 2.11695260794737 1.62219494319153 2.57847672580517 1.00000000000000
1.98465118243128 2.11349467948903 0.999999999999996 2.65578172203034 3.01897607767301 NaN 3.52998354037453 2.38833466800397 1.41223022448087 3.96751962825988
2.97995391814023 1.98589244648483 3.22884118121504 1.00000000000000 2.11695260794737 3.52998354037453 NaN 2.21498386821498 3.35954605769311 2.49207458085697
1.41286932041226 1.00000000000000 1.75480404405606 1.41514975278174 1.62219494319153 2.38833466800397 2.21498386821498 NaN 1.44673037867744 2.53341581840774
1.44150019470391 1.73544845449717 1 2.44463958996392 2.57847672580517 1.41223022448087 3.35954605769311 1.44673037867744 NaN 3.56150161869124
2.30703739193881 1.98611502337309 3.15259384660772 2.12446752335984 1.00000000000000 3.96751962825988 2.49207458085697 2.53341581840774 3.56150161869124 NaN];

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 6 Oct 2020
Try this:
rowsum = sum((A <= 1.5), 2);
result = find(rowsum >= 3)
producing:
result =
1
2
3
4
5
8
9
Those could be combined into one line:
result = find(sum((A <= 1.5), 2) >= 3)
I kept them separate for clarity.

  4 Comments

Show 1 older comment
Star Strider
Star Strider on 6 Oct 2020
As always, my pleasure!
It depends on what ‘don’t meet the criteria’ means, since there are two criteria.
Every row has more than 3 numbers greater than 1.5, so:
result = find(sum((A > 1.5), 2) >= 3)
returns all the rows.
This:
result = find(sum((A > 1.5), 2) < 3)
returns an empty array because the second criteriion was not met.
This:
result = find(sum((A <= 1.5), 2) < 3)
returns:
result =
6
7
10
that may be what you want, since it is the complement of the vector returned by the original test.
If you want a different result than any of these. please go into a bit of detail in describing it.
James Peach
James Peach on 6 Oct 2020
Sorry Im just seeing this, but I meant the 3rd case you showed which is the complement of the original vector. Thank you again!
Star Strider
Star Strider on 6 Oct 2020
That’s what I thought, although I wanted to explain the other options.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!