How can I select two out of 6 points for every page without loops?
Show older comments
I am trying to select two out of 6 points in an array whose size is 6x3xn. These two points are not in the same row on each page as I am trying to select those two of the six points that meet a certain condition. Let's assume that condition is that every coordinate is between 0 and 30.
Is there away to do this without loops?
8 Comments
Bruno Luong
on 10 Aug 2023
Edited: Bruno Luong
on 10 Aug 2023
"Let's assume that condition is that every coordinate is between 0 and 30. "
And does it garanty you get exactly 2 points out of 6?
lit
on 10 Aug 2023
Bruno Luong
on 10 Aug 2023
So how you want to deal if the number of rows is not 2 and varies from page to page ?
It seems the problem is not well defined.
lit
on 11 Aug 2023
Bruno Luong
on 11 Aug 2023
Edited: Bruno Luong
on 11 Aug 2023
So the number of pages that satisfies that (exactly 2 rows are find) would reduce. And you might keep track of which of those are not discarded, which are.
Your discription "... for every page" "...on each page" are missleading if not wrong.
Accepted Answer
More Answers (1)
Bruno Luong
on 15 Aug 2023
n = 4;
nsolperpage = 2;
X = randi(40, [6, 3, n])
mask = all(X >= 0 & X <= 30, 2);
[~,p] = find(reshape(mask, [], n));
count = accumarray(p, 1, [n 1]);
keep = count == nsolperpage;
extractpages = find(keep)
mask(:,:,~keep) = false;
Y = reshape(X([mask, mask, mask]), nsolperpage, 3, [])
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!