Why indexing returns values from first two columns and not from the whole matrix?
Show older comments
Hi everyone,
I've matched a 2 column vector (containing lat lon values respectively) from text file with 2 variables (also containing lat lon values) in matrix form. Output of this is
lat_txt = tab.lat; % extract the column of latitude values 3798x1 double
lon_txt = tab.lon; % extract the column of longitude values 3798x1 double
lat_i = imresize(M_lat,[2030 1354],'bilinear'); %M_lat is 406x270. So, I've interpolate it.
lon_i = imresize(M_lon,[2030 1354],'bilinear'); %M_lon is 406x270. So, I've interpolate it.
% matched the values from text file with the lat_i and lon_i
mask = ismembertol(lat_txt, lat_i) & ismembertol(lon_txt, lon_i); % gives 3798x1 logical (all values are true)
Next I've to done indexing to find out the values in another matrix 'z' that contains integers (0,1,2,3,4).
Vol = z(mask); % z = 2030x1354
The problem is Vol just returns values from 1st and 2nd column of z. Data in z is attached. It's first column contains 0. So, Vol shows 3798x1 where 1st row till row 2030 all are 0, then it starts getting values from 2nd column and first row of z. Why it's like this? Any help would be greatly appreciated.
Answers (1)
Walter Roberson
on 28 Sep 2021
0 votes
your ismembertol is going to return something with the same number of elements as lat_txt (first operand) but then you try to use that mask to index the larger z that is size of lat_i
If you reverse the order of operands in ismembertol then you would get something the size of lat_i
12 Comments
Zhou Ci
on 28 Sep 2021
Walter Roberson
on 28 Sep 2021
Your lat_txt is 3798x1 . You ismember(lat_txt) against an array, so you get back a 3798x1 vector of results. You use that 3798x1 logical vector to index into a 2030x1354 array. The first 2030 elements of the 3798x1 vector match the first column of the 2030x1354 array. The next 1768 elements of the 3798x1 vector match the first 1768 elements of the second column of z. There are no more elements of the 3798x1 vector left to match any other columns of ther 203x1354 array z.
for all the matched values in lat_txt I want values from z
Use
mask = ismembertol(lat_txt, lat_i) & ismembertol(lon_i, lon_txt);
The result will be 2030x1354
Zhou Ci
on 28 Sep 2021
Walter Roberson
on 28 Sep 2021
mask = ismembertol(lat_txt, lat_i) & ismembertol(lon_txt, lon_i);
Zhou Ci
on 28 Sep 2021
Walter Roberson
on 28 Sep 2021
What was the result, and how did it differ from what you wanted?
Walter Roberson
on 28 Sep 2021
Another possibillity:
[mask, idx] = ismembertol(lat_i, lat_txt) & ismembertol(lon_i, lon_txt);
zvals = nan(size(lat_i))
zvals(mask) = z(idx(mask));
Zhou Ci
on 28 Sep 2021
Walter Roberson
on 28 Sep 2021
Oh, right, that makes sense... ummm...
What you are asking to do... you need to rethink.
For each point, you want to know if the latitude matches anywhere in the other matrix, and if the longitude matches anywhere in the other matrix, and if so you want the z value at the location of match. But it is not necessarily the case that the location where the latitude mathes is the same where the longitude matches, so the index locations might be different, and then what would you do?
Zhou Ci
on 28 Sep 2021
Walter Roberson
on 28 Sep 2021
If I recall correctly, you already rejected the use of interp2() for this problem, even though using it would make a lot of sense.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!