"find" yields different results for linear vs 2D indexing

1 view (last 30 days)
Hi all. I have 2 2D matrices, and I want to find entries in 1 of these matrices that fulfill certain numerical criteria and put these into a different matrix containing those found entries and only 0 everywhere else. The straightforward way to do this is with the "find" function:
idx = find((t_total > 0) & (t_total < 1) & (s_total >= 0) & (s_total <= 1));
t_hit = zeros(size(t_total));
t_hit(idx) = t_total(idx);
Another idea I had was to use rows and columns, since that might come in handy later, i.e.:
[rows,columns] = find((t_total > 0) & (t_total < 1) & (s_total >= 0) & (s_total <= 1));
t_hit = zeros(size(t_total));
t_hit(rows,columns) = t_total(rows,columns);
Surprisingly though (at least to me), these do not yield the same results and I do not understand why. I checked the maximum value of t_hit and in the former case, as expected, I got values in the range of 0 to 1 (i.e. the range I restricted the indices to in "find"). In the latter case, however, I get values significantly outside of this range. Why?

Accepted Answer

Steven Lord
Steven Lord on 24 Aug 2022
You don't need to use find. You don't care where the elements that satisfy your criteria are located, all you care about is that you can address those elements. For this you can use logical indexing.
A = magic(4)
A = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
mask = (6 < A) & (A < 13)
mask = 4×4 logical array
0 0 0 0 0 1 1 1 1 1 0 1 0 0 0 0
B = zeros(size(A));
B(mask) = A(mask).^2
B = 4×4
0 0 0 0 0 121 100 64 81 49 0 144 0 0 0 0
You could create the equivalent of B using linear indices:
inds = find(mask)
inds = 6×1
3 6 7 10 14 15
C = zeros(size(A));
C(inds) = A(inds).^2
C = 4×4
0 0 0 0 0 121 100 64 81 49 0 144 0 0 0 0
isequal(B, C) % true
ans = logical
1
But this involves an extra call to the find function that is not necessary.
  3 Comments
Dominik Rhiem
Dominik Rhiem on 24 Aug 2022
Edited: Dominik Rhiem on 24 Aug 2022
There is actually a use case for knowing where exactly the elements are located (more specifically, the rows and columns, actually, which is why I was even trying the second version out), but I think I found a way around it. Thanks!
Cris LaPierre
Cris LaPierre on 24 Aug 2022
You can still find the (rows, columns) if that is information you need elsewhere. Just use sub2ind to turn them into a linear index for extracting/assigning.

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 24 Aug 2022
Edited: Cris LaPierre on 24 Aug 2022
For what I believe is your desired outcome, you need to use linear indexing (your first code).
The reason is because t_total(rows,columns) does not extract individual values from your variable. It extracts all values is all (row,column) pairs. For example
b=rand(5)
b = 5×5
0.3801 0.9445 0.5138 0.6155 0.1007 0.9441 0.6310 0.0504 0.4228 0.1774 0.0145 0.9736 0.0392 0.2197 0.8385 0.3983 0.5672 0.9594 0.7469 0.7523 0.9220 0.6189 0.1857 0.1837 0.6277
% This extracts a 3x3 matrix, not 3 individual numbers
b([1 3 4],[2 4 5])
ans = 3×3
0.9445 0.6155 0.1007 0.9736 0.2197 0.8385 0.5672 0.7469 0.7523
The same thing happens when making the assigment. The (row,column) indices do not represent individual elements, but instead a matrix of every row and column combination.
a=zeros(5);
a([1 3 4],[2 4 5])=b([1 3 4],[2 4 5])
a = 5×5
0 0.9445 0 0.6155 0.1007 0 0 0 0 0 0 0.9736 0 0.2197 0.8385 0 0.5672 0 0.7469 0.7523 0 0 0 0 0
The way to assign to individual elements is to use linear indexing.
idx = sub2ind(size(b),[1 3 4],[2 4 5]);
b(idx)
ans = 1×3
0.9445 0.2197 0.7523
c=zeros(size(b));
c(idx) = b(idx)
c = 5×5
0 0.9445 0 0 0 0 0 0 0 0 0 0 0 0.2197 0 0 0 0 0 0.7523 0 0 0 0 0

Tags

Community Treasure Hunt

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

Start Hunting!