variance of an array

10 次查看(过去 30 天)
Vc27
Vc27 2022-12-31
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

回答(2 个)

Naeimeh N
Naeimeh N 2022-12-31
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 个评论
Vc27
Vc27 2022-12-31
Is there a way to plot the gradient line on a scatter plot I have already?
Image Analyst
Image Analyst 2022-12-31
You didn't answer my questions below.
You can plot with the plot function.

请先登录,再进行评论。


Image Analyst
Image Analyst 2022-12-31
"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:

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by