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

In summary, the 'find' function can be used to locate values in a 2D, 3D, or 4D array, but it is not applicable to arrays with elements less than 0.5.
  • #1
Halsey
3
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
  • #2
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.
 
  • #3
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.)
 
  • #4
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!
 

What is the "find" function in MATLAB used for?

The "find" function in MATLAB is used to locate the indices of array elements that meet a certain condition. It is often used to filter and extract specific values from an array.

Can the "find" function be used on 4D arrays in MATLAB?

Yes, the "find" function can be used on 4D arrays in MATLAB. It can locate indices of elements that meet a condition in any dimension of the array.

What is the syntax for using the "find" function on a 4D array in MATLAB?

The syntax for using the "find" function on a 4D array in MATLAB is as follows:
indices = find(array, condition)
where "array" is the 4D array and "condition" is the logical expression that elements must meet.

Can the "find" function return multiple indices for a 4D array in MATLAB?

Yes, the "find" function can return multiple indices for a 4D array in MATLAB. If there are multiple elements that meet the condition, the function will return a vector of all the indices.

Are there any limitations to using the "find" function on 4D arrays in MATLAB?

One limitation of using the "find" function on 4D arrays in MATLAB is that it can only locate indices of elements that meet a condition within the array. It cannot perform operations on the array elements or modify the array itself.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
Replies
19
Views
1K
Back
Top