Why indexing returns values from first two columns and not from the whole matrix?

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)

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

Sir, that's correct. My Problem:
1) Lat_txt (from text file) contain lat lon values in the form of column vector.
2) Lat_i are interpolated values of lat lon from hdf file.
3) z is another variable from hdf file. (2030x1354 double)
Why I used Lat_txt first in ismembertol is because firstly I want to match lat lon (from txt file with lat lon from hdf file) and for all the matched values in lat_txt I want values from z.
But rather than getting values from whole z, I get values from first two columns of z.
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
Using the above line of code gives me error:
Matrix dimensions must agree.
mask = ismembertol(lat_txt, lat_i) & ismembertol(lon_txt, lon_i);
Unfortunately, this is something which I've done before.
What was the result, and how did it differ from what you wanted?
Another possibillity:
[mask, idx] = ismembertol(lat_i, lat_txt) & ismembertol(lon_i, lon_txt);
zvals = nan(size(lat_i))
zvals(mask) = z(idx(mask));
My Code with explanation:
% Text file contains two columns, lat and lon. I have extracted them:
lat_txt = tab.lat; % extract the column of latitude values (3798x1 double)
lon_txt = tab.lon; % extract the column of longitude values (3798x1 double)
%Hdf file has lat, lon and variable Radius (here named as z).
%Read lat lon and Radius
z = hdfread(hdf_filename, 'Radius'); % 2030X1354 double
lat = hdfread(hdf_filename, 'Latitude'); % 406x270 double
lon = hdfread(hdf_filename, 'Longitude'); % 406x270 double
%Interpolated the lat lon of hdf file
lat_i = imresize(lat,[2030 1354],'bilinear'); % 2030x 1354
lon_i = imresize(lon,[2030 1354],'bilinear'); % 2030x 1354
%match the lat lon values of text file with lat lon values of hdf file. Hdf
%file has lat lon for a large area, I just want lat lon of the text file
%(it has lat lon of area I want).
mask = ismembertol(lat_txt, lat_i) & ismembertol(lon_txt, lon_i); %3798x1 logical
% For each pixel of hdf file it has a value for Radius. But, I want values
% of Radius using lat and lon of text file, means I want to extract the
% values of Radius based on lat and lon of txt file.
Data = z(mask);
And Data gives me Radius values from just first 2 columns of z (Radius). I want it to give me from whole matrix. Is there anything I'm doing wrong while interpolating the lat lon?
@Walter Roberson Sir, from your last lines of code it returns following error
Error using &
Too many output arguments.
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?
I understand this. So, is there any other way to do this task? Means use the lat lon from text file and extract Radius values from hdf file?
If I recall correctly, you already rejected the use of interp2() for this problem, even though using it would make a lot of sense.

Sign in to comment.

Tags

Asked:

on 28 Sep 2021

Commented:

on 28 Sep 2021

Community Treasure Hunt

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

Start Hunting!