Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Adam Danz
Adam Danz le 2 Déc 2020
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 commentaires
Philippe Corner
Philippe Corner le 8 Août 2021
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
Adam Danz
Adam Danz le 9 Août 2021
I commented on the thread.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by