How could I read Index number of a third column matrix using elements of first two matrices and write them as a row matrix?
3 ビュー (過去 30 日間)
古いコメントを表示
I have A and B two 4x4 matrices, and C = 20x1 matrix.
How could I save result of
find(C>A(1,1) & C<B(1,1))
in the first coulmn of another matrix D so that when I go to (using loop)
find(C>A(1,2) & C<B(1,2))
it saves those data in second column of D and so on? The number of rows in first and second case may varry in real scenario.
For my actual purpose, I have A and B = 20x20 matrix. and C = around100000 x 1.
Your help would be highly appreciated.
0 件のコメント
回答 (1 件)
Adam Danz
2020 年 9 月 23 日
編集済み: Adam Danz
2020 年 9 月 28 日
Use logical indexing which is similar to what you're already doing but without the find(). That way all of the index vectors will have the same length. Otherwise, you cannot store vectors of different length within a matrix. For example, the first find() may only result in 3 matches while the second find() may result in 20 matches. Logical indexing is usually easier to work with, anyway.
Demo
A = [1,6; 5,13];
B = [7,10; 11,19];
C = (1:20)';
D = nan(numel(C), size(A,2));
for i = 1:size(D,2)
D(:,i) = C>A(1,i) & C<B(1,i);
end
As you can see in the result below, D(:,n) identifies the matches for C>A(1,n) & C<B(1,n). If you need the row numbers that are returned by find(), use find(D(:,n)).
Result
D =
0 0
1 0
1 0
1 0
1 0
1 0
0 1
0 1
0 1
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Multidimensional Arrays についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!