Searching a matrix and eliminating values below a certain value
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Image Analyst
2020 年 4 月 23 日
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.
採用された回答
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 件)
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!