Calculating Table Elements with Equations A & B

  • Thread starter Thread starter loukoumas
  • Start date Start date
  • Tags Tags
    Elements Table
Click For Summary

Discussion Overview

The discussion revolves around calculating elements of a 100x300 table using two different equations, A and B, within nested for loops in MATLAB. Participants explore methods to efficiently implement conditional calculations for specific elements of the table.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes a method using nested for loops to calculate elements with equation A, while needing to apply equation B to specific elements.
  • Another participant suggests using an array to store the indices of elements that require equation B, proposing the use of the 'ismember' function to check if (i,j) is in that list.
  • A further suggestion includes creating a 100x300 array of function names for equations A and B, allowing for dynamic function selection based on the element's indices.
  • There is mention of using a structure array of function handles as a potential alternative approach.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method to implement the calculations, with multiple approaches being proposed and no definitive solution agreed upon.

Contextual Notes

Some participants express uncertainty about the limitations of MATLAB functions and the feasibility of certain proposed methods, indicating that further exploration may be needed.

loukoumas
Messages
14
Reaction score
0
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!
 
Physics news on Phys.org


loukoumas said:
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 ?

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
 

Attachments

  • phys - 12 05 29 choosing functions 01.jpg
    phys - 12 05 29 choosing functions 01.jpg
    29.6 KB · Views: 445


NemoReally said:
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.

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
 


Thanks a lot mate!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
1K
Replies
1
Views
18K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 0 ·
Replies
0
Views
2K