I have a numeric 2D array called A with time in the first column (from zero to 2 minutes every 0.01 seconds). I want to find the rows that match times = 10 : 10 : 100;
When I type find(10==A(:,1), I get 1001
When I type find(20==A(:,1), I get 2001
Why when I type find(times==A(:,1) does it give these values: [1001, 12957, 24913, 36869, ...]?
These values of times (which are 10, 20, 30, ...) are at rows [1001, 2001, 3001, 4001, ...], so that's what I expected.
I know I don't really need a find() in most situations because I can use indexing, but that doesn't work either:
plot(A(A(:,1)==times,3),A(A(:,1)==times,2),'rs')
gives error msg: The logical indices in position 1 contain a true value outside of the array bounds.
because A(:,1)==times gives value of 12957 for the second index which is outside of array A bounds.

2 comentarios

madhan ravi
madhan ravi el 13 de Jul. de 2019
size(times) % what does these show?
size(A)
Upload your datas as .mat file
Image Analyst
Image Analyst el 13 de Jul. de 2019
Are they integers or floating point?
See the FAQ for how to test for equality with doubles.

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 13 de Jul. de 2019

0 votos

You have encountered ‘automatic implicit expansion’ introduced in R2016b. The result you get from your equality expressions will be an (Nx10) array, where ‘N’ is the row size of ‘A’ (720001 in my reconstruction here) and 10 is the size of ‘times’.
The only way to get the result you want are either to compare each value of ‘times’ in a loop, or more efficiently to use the accumarray function.
Try this:
A = [0:0.01:7200; rand(size(0:0.01:7200))]'; % Create Matrix
times = 10:10:100; % Create Vector
Out = accumarray((1:numel(times))', times, [], @(x){A(A(:,1)==x,:)})
Reveal = cell2mat(Out)
With my random matrix, this gave the correct results, when I checked them against the actual rows of ‘A’.
You can then plot them as:
figure
plot(Reveal(:,1), Reveal(:,2),'rs')
grid
I named the double matrix ‘Reveal’ to explain what it is. Name it anything you want.

6 comentarios

Mike D.
Mike D. el 13 de Jul. de 2019
It should be easier than this to identify which rows match. So, it used to work in old Matlab until they changed it?
Star Strider
Star Strider el 13 de Jul. de 2019
I am not sure if it worked in R2016a or earlier (I no longer have easy access to those versions), however I would assume it would throw a ‘matrix dimensions must agree’ error, since ‘times’ and ‘A’ have different row sizes, even if ‘times’ is transposed to a column vector.
The accumarray (or less efficiently in this situation the for loop) approach is likely the only one that would work either way.
Star Strider
Star Strider el 13 de Jul. de 2019
Mike D’s Answer moved here:
size(A) is 10956 rows and 12 columns and A is double array.
size(times) is 1 row and 11 columns and double array.
Here is another example, much simpler:
B = 1:13;
C = 2:2:6;
find(C==B) gives error "Matrix dimensions must agree"
find(C==B') returns [2; 17; 32] why is that?
I expected find(C==B) to return [2, 4, 6].
I could use [~,~,vertexID] = intersect(B,C) and that works.
so does [~, ~, vertexID] = intersect(times,A)
I just expected it to be easier.
Star Strider
Star Strider el 13 de Jul. de 2019
find(C==B') returns [2; 17; 32] why is that?
The equality ‘C==B'’ returns a (13x3) logical array:
0 0 0
1 0 0
0 0 0
0 1 0
0 0 0
0 0 1
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
and find with one output produces indices for a linearly indexed array of that (13x3) logical array. (Linear indexing considers the array a one-column vector. I encourage you to explore the documentation on Matrix Indexing to understand that, since the documentation explains it much better than I could.)
If you ask for both the row and column indices for that same array:
[r,c] = find(C==B')
the result is a more easily interpreted:
r =
2
4
6
c =
1
2
3
The intersect calls would work, and so likely would ismember (or ismembertol) and other functions. It depends on what you want to do.
Meanwhile, one accumarray call works in the problem you posted, to return all relevant columns in the selected rows.
Mike D.
Mike D. el 13 de Jul. de 2019
ok, thanks, I just wanted to identify which rows in A that match "times" so that I can plot only those rows in A. I see I have three options now.
Star Strider
Star Strider el 13 de Jul. de 2019
My pleasure.
If my Answer helped you solve your problem, please Accept it!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2019a

Etiquetas

Preguntada:

el 13 de Jul. de 2019

Comentada:

el 13 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by