How can I select two out of 6 points for every page without loops?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
Bruno Luong
2023년 8월 11일
편집: Bruno Luong
2023년 8월 11일
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.
채택된 답변
Jan
2023년 8월 10일
n = 2;
X = randi(40, 6, 3, n)
mask = all(X >= 0 & X <= 30, 2)
Y = reshape(X(cat(2, mask, mask, mask)), [], 3)
댓글 수: 6
Bruno Luong
2023년 8월 15일
편집: Bruno Luong
2023년 8월 15일
OK this looks correct. But I don't think you need permute and transpose at all. It seems like a side effect of your though trying to fix Jan's coden but that is not relevant.
n = 4;
nsolperpage = 2;
X = randi(40, [6, 3, n])
mask = all(X >= 0 & X <= 30, 2);
keep = sum(mask,1) == nsolperpage;
extractpages = find(keep)
mask(:,:,~keep) = false;
Y = reshape(X([mask, mask, mask]), nsolperpage, 3, [])
추가 답변 (1개)
Bruno Luong
2023년 8월 15일
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, [])
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Author Block Masks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!