MATLAB Answers

Searching a matrix and eliminating values below a certain value

2 views (last 30 days)
Hi,
I have a matrix, A. Each column is a set of consecutive numbers of different lengths, padded with nan because I extracted from a cell array. I have a column vector, B. The numbers in both are indices of curves I am trying to define, and they are both in increasing order. I want to make the values of B the starting points of the consecutive numbers in A, for each column.
I want to go through each column of A and find where there is also a point from B, and then get rid of all the values in that column of A below that overlapping point. I am trying to write a 'for' loop with 'ismember' but I either get error messages or it doesn't actually do anything. The output would be either a cell array or a matrix.
%for example
A=[ 1 7 20
2 8 21
3 9 22
4 10 23
nan 11 24
nan nan 25
nan nan 26 ...]
B=[2 7 8 22]
C=[ 2 7 22
3 8 23
4 9 24
nan 10 25
nan 11 26 ...]
I've tried the for loop to find where A and B overlap but I can't even get that far
for i=1:size(A,2)
C(i)=ismember(B(:,i),A)
end
Please help either with this method or any one that works!
Thanks

  2 Comments

Image Analyst
Image Analyst on 23 Apr 2020
First of all B is a row vector, not a column vector, and it does not have the same number of rows or columns as A. So I guess you're going to search the entire B for any value that matches any value in columns of A? So what if a column of A has 2 or 3 elements of B in it? Like column 2 of A has both a 7 and an 8, like B. So do you want to take the first match, or second match? Then you said "get rid of all the values in that column of A below that overlapping point" but if anything it looks like you're getting rid of rows of A in that column ABOVE, not below. Please explain in more detail what your algorithm is.
Teddy Fisher
Teddy Fisher on 23 Apr 2020
Hi thanks!
  1. Yes I could search B for matches to A, but I tried to search A because I wanted the index of the matching point to be in terms of the A matrix. But I'm not exactly sure which way to do it.
  2. If a column has 2 matches, I want to take the first match and use that as the "starting point," as it were.
  3. Yes, it would be all values above the match. Each column of A is in ascending order, so I want to get rid of all values above the starting point/match (but numerically less than) in each column.
Thanks for your help!

Sign in to comment.

Accepted Answer

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 24 Apr 2020
Code is Not optimized, but works fine
maxRow = 1;
for i=1:size(A,2)
BIndex = ismember(B,A(:,i));
BIndex = find(BIndex, 1, 'first');
indexA = find(A(:,i) == B(BIndex));
D = A(indexA:end,i);
E{i} = D(~isnan(D));
if (maxRow < length(E{i}))
maxRow = length(E{i});
end
end
C = NaN(maxRow,size(E,2));
for i = 1:size(E,2)
C(1:length(E{i}),i) = E{i};
end
disp(C)

  2 Comments

Teddy Fisher
Teddy Fisher on 24 Apr 2020
Hi, thanks! It totally works!
None of the rest of the script it is going into is optimized either so it will fit right in.
Thank you so much, this really helped me out.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!