variance of an array

16 views (last 30 days)
Vc27
Vc27 on 31 Dec 2022
Commented: Image Analyst on 31 Dec 2022
Hi, I have a for loop in which i need to assign the value for the variance to a component of a 200 component array. I want to put a value for the variance in a different element each time I go through the loop so that I can keep track of the each variance value calculated. Once the loop has finished I want to have a 200 component array that has the angel for my axis. I want to then find which of these components has the greatest variance and then plot its gradient on my plot - anyone have any idea how to approach this? thanks in advance

Answers (2)

Naeimeh N
Naeimeh N on 31 Dec 2022
To approach this problem, you can create an array to store the variance values, and then update the appropriate element of the array at each iteration of the loop. See the below example:
% Define the size of the variance array
variance_array = zeros(1, 200);
% Loop through the 200 elements
for i = 1:200
% Calculate the variance for the current element. replace this function
% with whatever that calculates the variance.
variance = calculate_variance(i);
% Store the variance value in the appropriate element of the variance array
variance_array(i) = variance;
end
Unrecognized function or variable 'some_function'.
% Find the element with the greatest variance
[~, max_variance_index] = max(variance_array);
% Plot the gradient for the element with the greatest variance
plot_gradient(max_variance_index);
  2 Comments
Vc27
Vc27 on 31 Dec 2022
Is there a way to plot the gradient line on a scatter plot I have already?
Image Analyst
Image Analyst on 31 Dec 2022
You didn't answer my questions below.
You can plot with the plot function.

Sign in to comment.


Image Analyst
Image Analyst on 31 Dec 2022
"I have a for loop in which....." <= you forgot to show us your loop!
And include the 200 element array in a .mat file so we can read it in. What is a "component"? Is the array a cell array and the component is a cell with some array of multiple values in it? Because if the component is a single number, the variance of a single number is zero.
And if you're talking about a 200 element numerical array, the variance of that array is whatever it is. I don't know how you can "put a value for the variance in a different element" of the component array. That would change the array itself, and also the variance of that array. For example
ca = rand(1,200); % "Component array"
v = var(ca) % Get variance of all 200 elements.
v = 0.0820
% Change element 2 to be 5000
ca(2) = 5000;
v = var(ca) % It will be different of course
v = 1.2498e+05
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Categories

Find more on Creating and Concatenating Matrices 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!