# How could I read Index number of a third column matrix using elements of first two matrices and write them as a row matrix?

15 views (last 30 days)
Ganesh Pandey on 23 Sep 2020 at 17:09
Edited: Adam Danz on 28 Sep 2020 at 17:05
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.

Adam Danz on 23 Sep 2020 at 17:27
Edited: Adam Danz on 28 Sep 2020 at 17:05
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

### Community Treasure Hunt

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

Start Hunting!

Translated by