MATLAB Answers

Searching a matrix and eliminating values below a certain value

조회 수: 2(최근 30일)
Teddy Fisher
Teddy Fisher 2020년 4월 23일
댓글: Mrutyunjaya Hiremath 2020년 4월 24일
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
Teddy Fisher
Teddy Fisher 2020년 4월 23일
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!

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

채택된 답변

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath 2020년 4월 24일
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

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by