refer to variable within matrix calculation
1 view (last 30 days)
Show older comments
is it possible to use values from variable A in a matrix calculation of variable B? what I want to do is calculate the constant error of a certain variable. to do this I need the mean of 16 rows from variable A (as a start). I tried this:
constantError(:,5) = mean( output( constantError(:,1)*16-15: constantError(:,1)*16,9 ) )
basically I take want to take 16 values from column 9 of output and place the mean of these values in column 5 of constantError. I use the first row of constantError as a counter, it gives the number of the row.
when I run this every row of column 5 of constantError contains the mean of the first 16 rows of column 9 of output. how can I fix this?
0 Comments
Answers (2)
BhaTTa
on 5 Jan 2025
Hey @jelle, I assume that you wan to calculate the mean of specific segments of data from a column in output and placing these means into constantError, you need to iterate over each row of constantError and compute the mean for each segment separately. It seems like you are trying to use a vectorized approach, but since each row requires a distinct mean calculation, a loop will be a good option.
0 Comments
Walter Roberson
on 5 Jan 2025
constantError(:,5) = mean( output( constantError(:,1)*16-15: constantError(:,1)*16,9 ) )
First the vector result constantError(:,1)*16-15 is calculated. Then the vector result constantError(:,1)*16 is calculated. Then the ":" operator acts on the two vector results. The ":" operator acting on non-scalar results is defined to be the same as acting on the first member of the array, so this is the same as
constantError(:,5) = mean( output( constantError(1,1)*16-15: constantError(1,1)*16,9 ) )
If you were hoping that it would process first constantError(1,1)*16-15: constantError(1,1)*16 and then constantError(2,1)*16-15: constantError(2,1)*16 and then constantError(3,1)*16-15: constantError(3,1)*16... sorry, it does not work that way.
If hypothetically the : operator worked on vectors, so that the ":" operator generated
[constantError(1,1)*16-15: constantError(1,1)*16
constantError(2,1)*16-15: constantError(2,1)*16
constantError(3,1)*16-15: constantError(3,1)*16
etc
]
then that would be a full array of subscripts that would be looked up as the first subscript of output(), and the result would be one large array of output() values... with mean() then operating on the entire large array, which is unlikely to be what you wanted. So this code would likely be inappropriate even if the ":" operator was vectorized (which it is not.)
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!