How to find a particular value from 3-d matrix?
1 view (last 30 days)
Show older comments
Keegan Carvalho
on 18 Mar 2020
Commented: Walter Roberson
on 19 Mar 2020
Hey MATLAB universe!
I have a netcdf file of sea surface temperatures (360x40x444) and found the mean of it. I have obtained a 2-d matrix and need help with the following:
I want to find out how many cells have a particular value (say, 1.75) from the entire 360x40 matrix.
My main aim is to find how many grid points (and what area) is covered by a temperature of 1.75 degree celsius. I have attached a .mat file.
Thanks!!
0 Comments
Accepted Answer
Walter Roberson
on 19 Mar 2020
None of the values are 1.75. The closest is 1.74979725818628
tolerence = 0.01;
target = 1.75;
nnz(abs(mean(:)-target)<tolerence)
%or
tolerence = 0.01;
target = 1.75;
nnz(ismembertol(mean, target, tolerence, 'datascale', 1))
The advantage of the second of these is that target could be a vector of values, each of which would be searched for:
tolerence = 0.01;
target = [1.75, 1.1];
[isinrange, idx] = ismembertol(mean, target, tolerence, 'datascale', 1);
nnz(idx==1)
nnz(idx==2)
0 Comments
More Answers (1)
dpb
on 19 Mar 2020
If you're certain the value 1.75 exactly exists in the file and that's all you want exactly (to machine precision) then simply
V=1.75;
[ir,ic]=find(sst==V);
does the trick. As floating point differences can cause 1.75+/-2.2E-16 to not match because is >eps(1.75) away from 1.75, you may find
tol=1E-5; % say, how close is "close enough"???
V=1.75;
[ir,ic]=find(ismembertol(sst,V,tol)); % find those within tolerance, tol
ismembertol is just "syntactic sugar" for writing the explicit test for abs(A-V)>tol
4 Comments
Rik
on 19 Mar 2020
That is something that still confuses me about the ismembertol function. Why would you not want to use absolute tolerances? It makes more sense to me to allow the relative tolerance as an optional parameter, instead of how the function currently works.
Walter Roberson
on 19 Mar 2020
If you want to express
then you do not generally want to have to know their range ahead of time in order to construct an absolute tolerance, and you do not want to have to keep track of which vector elements are which range in order to know what the appropriate absolute tolerance is for each one. You want to know if A is the same as B to "within round-off" where "within round-off" is related to a small multiple of eps of the one with the larger absolute value -- or to within the relative fraction if that is specified.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/278265/image.png)
ismembertol(A,B,0.001) -- are they the same to within 1 part in 1/1000?
ismembertol(A,B,0.001,'datascale', 1) -- are they the same to within 0.001
Asking if two numbers are the same to within some absolute tolerance is common, it it does seem to be a bit ackward to have to spell out 'datascale', 1 for that common case. Perhaps an ismemberabstol() would have been a useful interface.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!