Calculating Table Elements with Equations A & B

In summary, you can use two for loops to calculate every element of a 100x300 table, but if you need to calculate a few elements with a different equation, you can use an if and elseif function. You can also use a function to determine whether (i,j) is in a list of 'alternative' element indices.
  • #1
loukoumas
15
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
  • #2


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: 390
  • #3


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
 
  • #4


Thanks a lot mate!
 
  • #5


Hello there!

Thanks for sharing your method for calculating the elements of a table using equations A and B. Your approach using nested for loops and if statements is a valid way to handle this type of problem. However, there may be other ways to accomplish this task, depending on the specific requirements and constraints of your project.

One alternative approach could be to create a separate function for equation B and use it within your existing for loops. This would help to make your code more modular and easier to read, as well as potentially reducing the number of if statements needed. Another option could be to use logical indexing to apply equation B to specific elements of the table, rather than checking each individual element with if statements. This could potentially be more efficient for larger tables.

In general, there are many different approaches to solving a problem and it is important to consider factors such as efficiency, readability, and flexibility when choosing a method. It may be helpful to discuss your specific project and requirements with a colleague or supervisor to determine the best approach for your situation. I wish you the best of luck with your calculations!
 

1. What is the purpose of calculating table elements with equations A & B?

The purpose of calculating table elements with equations A & B is to determine the values or quantities of the elements in a given table. This is often done in scientific studies or experiments to analyze and understand the relationships between different variables.

2. What are equations A & B?

Equations A & B refer to two mathematical expressions that are used to calculate the elements in a table. These equations can be derived from scientific principles or theories and are often specific to the type of table being analyzed.

3. How do you choose which equation to use for a specific table element?

The equation used for a specific table element depends on the type of data being analyzed and the relationships between the variables. It is important to carefully consider the variables in the table and choose the appropriate equation that best represents the relationship between them.

4. Can equations A & B be used for any type of table?

No, equations A & B are specific to the type of table being analyzed. For example, if the table contains data on chemical reactions, the equations used would be different from those used for a table containing data on motion.

5. How accurate are the calculations using equations A & B?

The accuracy of the calculations using equations A & B depends on the accuracy of the data input and the assumptions made in the equations. It is important to carefully consider the variables and their uncertainties to ensure the most accurate results.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
702
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • Electrical Engineering
Replies
3
Views
744
  • Programming and Computer Science
Replies
8
Views
1K
Replies
4
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K
Replies
0
Views
2K
Back
Top