# Searching a matrix and eliminating values below a certain value

2 views (last 30 days)
Teddy Fisher on 23 Apr 2020
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
Thanks
##### 2 CommentsShow 1 older commentHide 1 older comment
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.

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 CommentsShow 1 older commentHide 1 older comment
Mrutyunjaya Hiremath on 24 Apr 2020
Welcome :)

### Categories

Find more on Agriculture in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!