How create a matrix that matches a condition in comparison with other matrix?
4 次查看(过去 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.
0 个评论
采纳的回答
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,:)
4 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!