MATLAB How can I use the find function to replace missing values in a 4D array?

AI Thread Summary
The discussion centers around optimizing the process of replacing missing values in a 4D array of climate variables in MATLAB. The original code uses nested loops and conditional statements to identify and replace values greater than 1000 with NaN, which is inefficient given the size of the dataset. Suggestions include using the 'find' function to streamline the process, as it can handle multi-dimensional arrays and potentially improve performance. It is noted that a more efficient approach could be to directly assign NaN to the elements exceeding 1000, using the syntax `temp(temp > 1000) = NaN;`, which avoids the need for division by zero and eliminates warnings. Additionally, the use of MATLAB's `tic` and `toc` functions is recommended for performance comparison between the current method and potential optimizations.
Halsey
Messages
3
Reaction score
0
Hi all-

I have a question regarding using the find function as opposed to if statements in finding values in a 4D array. These are climate variables, such as temperature (in Kelvin). Missing values are represented as 10^15, and I want to change them to NaN which plots much nicer. I have the following code so far:


for i = 1:288 %all longitudes
for j = 1:144 %all latitudes
for k = 1:42 %all heights
for l = 1:8 %all times
if temp(i,j,k,l) > 1000
temp(i,j,k,l) =0/0;
end
end
end
end
end

and it is doing what I want, but I fear that it is not very efficient because i have 11 climate variables total to do this for, and the if conditionals waste a lot of time. i have used the find function on 1D and 2D arrays, but I do not know how it works, if at all, on 4D arrays. Any help would be great! Thanks in advance.
 
Physics news on Phys.org
Take a look at the MATLAB documentation for the 'find' function:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/find.html

You can either get linear indices (i.e. your 4-d array gets squashed down into a single vector), or you can get indices returned to you by dimension (take a look at one of the provided examples; it should generalize to more than two dimensions).

I'm not sure that it'll be any faster (maybe with some parallelization) but you could use 'tic' and 'toc' to see how it compares with the looping and scanning method. If it goes too fast, you may be able to do it several times inside a loop, and then divide the result by the number of times you looped.
 
Is this a legal statement in MATLAB:
temp(temp>1000)=0/0;

I'm more familiar with Python's numpy ( http://www.scipy.org/NumPy_for_Matlab_Users )
and they refer to an example under Linear Algebra Equivalents:
MATLAB: a(a<0.5)=0
NUMPY: a[a<0.5]=0
Notes: a with elements less than 0.5 zeroed out
(If this example only works for 2D arrays, you might have to somehow reshape your temp array or loop over slices to do this operation.)
 
robphy said:
Is this a legal statement in MATLAB:
temp(temp>1000)=0/0;

I'm more familiar with Python's numpy ( http://www.scipy.org/NumPy_for_Matlab_Users )
and they refer to an example under Linear Algebra Equivalents:
MATLAB: a(a<0.5)=0
NUMPY: a[a<0.5]=0
Notes: a with elements less than 0.5 zeroed out
(If this example only works for 2D arrays, you might have to somehow reshape your temp array or loop over slices to do this operation.)

I was going to make a point about this. Instead of doing a 0/0 and getting a warning about your division by 0 (which you can turn off by writing "warning off" at the top of your m-file, or before you execute your statements), you can set them directly to NaN, with an assignment:

>> bob = 0/0;
>>% gives an error
>> bob = NaN;
>>% no error!
 
Back
Top