How create a matrix that matches a condition in comparison with other matrix?

조회 수: 6 (최근 30일)
The matrix A is given by several repeated values of A(:,1) positions with different A(:,2) values like:
clc;clear all
A=[
1 10
1 9
1 8
1 7
1 6
2 15
2 14
2 13
2 12
2 11
3 18
3 17
3 16
3 15
3 14
4 12
4 11
4 10
4 9
4 8];
scatter(A(:,1),A(:,2),'filled','r')
Represented by the following red dots:
The matrix B, represents 1 [X,Y] combination for each B(:,1) as:
%%
B=[
1 8
2 14
3 11
4 12];
hold on
scatter(B(:,1),B(:,2),'filled','b')
How can I eliminate all the values extrictly higher than any B(:,2) for each B(:,1) and keep only same or lower values getting a C matrix like:
%%
C=[
1 8
1 7
1 6
2 14
2 13
2 12
2 11
4 12
4 11
4 10
4 9
4 8];
scatter(C(:,1),C(:,2),'filled','g')
green dots represent the wanted points to keep.

채택된 답변

Adam Danz
Adam Danz 2020년 12월 2일
A=[
1 10
1 9
1 8
1 7
1 6
2 15
2 14
2 13
2 12
2 11
3 18
3 17
3 16
3 15
3 14
4 12
4 11
4 10
4 9
4 8];
B=[
1 8
2 14
3 11
4 12];
idx = cell2mat(arrayfun(@(i){A(:,1)==B(i,1) & A(:,2)<= B(i,2)},1:size(B,1)));
[rowIdx,~] = find(idx);
C = A(rowIdx,:)
C = 12×2
1 8 1 7 1 6 2 14 2 13 2 12 2 11 4 12 4 11 4 10
  댓글 수: 4
Philippe Corner
Philippe Corner 2021년 8월 8일
Hello Adam, could you check this question? I think you may help me to find the best way to solve it.. https://it.mathworks.com/matlabcentral/answers/894752-how-to-assign-values-to-a-mesh-based-on-xyzc-points

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by