How create a matrix that matches a condition in comparison with other matrix?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
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,:)
4 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!