Finding the values right after and right before some values in a matrix

조회 수: 11 (최근 30일)
mr mo
mr mo 2017년 11월 6일
댓글: Guillaume 2017년 11월 8일
Hi. Suppose I have the matrix A of size (m*n).
A = [12 44
93 43
128 44
145 41
180 41
220 40
280 40];
also I have the vector V e.g.
V = [13 20 70 90 95 100 102 110 129 130 145 158 170 185 190 200 207 220 270 280 285 290];
I want to find which one of the rows of matrix A based on its first column values is right after and right before the values of vector V, and if one of the values of the first column of matrix A is equal to the values of vector V, I want to find that row.
For example :
A(2,1) is right before V(5) and V(6) and V(7) and V(8)
A(3,1) is right after V(5) and V(6) and V(7) and V(8)
and in case of equality I want to find this
A(4,1) = V(11)
A(5,1) = V(18)
A(7,1) = V(20)
In other words, I want to find exactly which members of vector V are between which members of first column of matrix A with their indices like this :
V(1) and V(2) and V(3) and V(4) is between A(1,1) and A(2,1)
Thanks for your help.
  댓글 수: 2
KL
KL 2017년 11월 6일
what do you mean by "right before"? If I want to explain it myself I would say 93 is right before 94 (and 94 only!). It is not right before 95!
and right after is the immediate next number, not every number after it.
mr mo
mr mo 2017년 11월 6일
I am looking for this. For example
V(1) and V(2) and V(3) and V(4) is between A(1,1) and A(2,1)

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

답변 (2개)

KL
KL 2017년 11월 6일
편집: KL 2017년 11월 6일
res = find(V>A(1,1) & V<A(2,1))
for all elements,
res = arrayfun(@(x,y) find(V>x & V<y), A(1:end-1,1), A(2:end,1),'uni',0)
  댓글 수: 9
mr mo
mr mo 2017년 11월 7일
Thanks a lot. You are right about the "we can make a matrix if the number of elements is same for each row". I just forgot that. This is what I need.
mr mo
mr mo 2017년 11월 7일
편집: mr mo 2017년 11월 7일
Suppose I want to find that only V(10)=130 is lying between which two members of A(:,1). How can I do this process to have the out cell array like your last code?
And in case of equality how can I have the out cell array like the case of inequality? Thanks a lot.

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


Guillaume
Guillaume 2017년 11월 7일
An alternative method that does not involve loops (or arrayfun) but is not necessary faster than KL's:
comp = abs(A(:, 1) - V);
comp(A(:, 1) > V) = inf;
[~, beforeVidx] = min(comp, [], 2);
isequaltoV = A(:, 1) == V(beforeVidx)';
%for display only:
table(A, beforeVidx, isequaltoV)
  댓글 수: 4
mr mo
mr mo 2017년 11월 7일
Also this error was happened.
comp(A(:, 1) > V) = inf;
Error using >
Matrix dimensions must agree.
Guillaume
Guillaume 2017년 11월 8일
Same issue, same solution, use bsxfun:
comp(bsxfun(@gt, A(:, 1), V)) = inf;

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by