Storing calculations into a list

The length of xval is also a scalar, so when you multiply them together, you get a scalar result. Similarly, nansum of yval also returns a scalar, so when you multiply it with the previous result, you still get a scalar. This is why numerator is not stored as a 5x1 array like xval and yval.
  • #1
Tone L
73
7
I have a code that loops through and does some computation on 5 random values from my lists of data (2187 rows long).

for i = 1:3
randomdata = round(rand(5,1).*2187);

xval = x(randomdata)
yval = y(randomdata)
numerator = length(xval).*nansum(xval.*yval) - (nansum(xval).*nansum(yval))
end

my output on the screen is as follows: 5 random values stored in xval & yval
xval =
3.3
3.2
6.4
4.3
1.2

yval =
13
33
83
42
9

numerator =
-.34
numerator =
-.12
numerator =
-.45
My question is why isn't numerator being stored as a 5x1 array like xval and yval??
 
Physics news on Phys.org
  • #2
Anthony LaRosa said:
My question is why isn't numerator being stored as a 5x1 array like xval and yval??
Because nansum of a 5x1 array returns a scalar.
 

What is the purpose of storing calculations into a list?

Storing calculations into a list allows for easier organization and manipulation of data. It also allows for efficient retrieval of past calculations for future use.

How do you store a calculation into a list?

To store a calculation into a list, you would first create an empty list using square brackets []. Then, you can use the append() function to add the calculation result to the list.

What types of calculations can be stored into a list?

Any type of calculation that produces a value can be stored into a list. This includes simple math operations, as well as more complex calculations involving functions or equations.

Can you store multiple calculations into one list?

Yes, you can store multiple calculations into one list by using the append() function to add each calculation result to the list. You can also use list comprehension to add multiple calculations to a list at once.

How do you access and use the stored calculations from a list?

To access stored calculations from a list, you can use index notation to retrieve the specific calculation result you need. You can also use loops or list comprehension to access and use multiple calculations from a list.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
889
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
865
  • Science and Math Textbooks
Replies
3
Views
810
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Replies
54
Views
650
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
945
Back
Top