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

Calculating, among the points of a mesh, only a few points with a different equation.

  1. May 28, 2012 #1
    Hello again!

    Let's say that i am calculating every element of a 100X300 table by using equation A
    I use two for loops (i m doing this in matlab)
    for i=1:100
    for j=1:300
    element(i,j)=... (it is the equation A)
    end
    end
    equation A involves the neighboring elements
    What if, a few elements of the table must be calculated with a different equation, equation B and this must happen inside the two for loops

    for example elements (49,81) (49,82)
    (50,81) (50,82)
    (51,81) (51,82)
    (52,81) (52,82)
    i could use if and elseif function for every single element, like
    if i==49 && j==81
    element(i,j)=...equation B
    elseif i==50 && j==81
    element(i,j)=...equation B
    ...

    ...

    end
    Is there another, better way to do it ???

    Thanks a lot!
     
  2. jcsd
  3. May 29, 2012 #2
    Re: calculating, among the points of a mesh, only a few points with a different equat

    I'm not sure about 'better', but you could put the list of 'alternative' element indices in an array and then use a function to determine whether (i,j) is in that list. I don't use Matlab, but a very quick look at the documentation suggests that ismember may do the trick (it will return a vector of all zeros if (i,j) isn't in The List.

    I've attached a picture of what I mean implemented in Mathcad.

    I'm presuming that you can't take B outside of the loop.

    NR
     

    Attached Files:

  4. May 29, 2012 #3
    Re: calculating, among the points of a mesh, only a few points with a different equat

    Forgot to add, and I think Matlab should be able to do this, an alternative would be to create a 100x300 array whose elements are the function name of f, and then change the 'special' cases to the function name of g. In your main loop, you then look up the appropriate function for element (i,j) in the function array.

    ... a quick glance at the Matlab documentation suggests that, although this might be possible, a better route to go might be to create a structure array of f's and g's handles.

    NR
     
  5. May 31, 2012 #4
    Re: calculating, among the points of a mesh, only a few points with a different equat

    Thanks a lot mate!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Calculating, among the points of a mesh, only a few points with a different equation.
  1. Dew point/Bubble Point (Replies: 0)

Loading...