Storing calculations into a list

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 1K views
Tone L
Messages
72
Reaction score
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
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.