Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB find function 4D array

  1. Jun 23, 2009 #1
    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 alot 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.
     
  2. jcsd
  3. Jun 24, 2009 #2

    MATLABdude

    User Avatar
    Science Advisor

    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.
     
  4. Jun 24, 2009 #3

    robphy

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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.)
     
  5. Jun 24, 2009 #4

    MATLABdude

    User Avatar
    Science Advisor

    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!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MATLAB find function 4D array
  1. Functions and arrays. (Replies: 1)

Loading...