How to compute the variance of an array progressively taking away the highest values?

28 Ansichten (letzte 30 Tage)
Hello,
I have an array of N values, out of which I want to exclude the highest ones. I wanted to visualise how the variance changes by progressively taking a smaller and smaller sample size. The end result would be a plot in which the X-axis represents the amount of individual observations I am taking away, and the Y axis the variance of the remaning data.
I manage to do it, but only by deleting an arbitrary number of N values, which doesn't allow me to create a loop.
Is it possible to loop it?
  2 Kommentare
Rik
Rik am 5 Sep. 2021
Do you want to always remove the highest value? If so, you can sort your array and use simple indexing in a for loop.
Happy_Shovel
Happy_Shovel am 5 Sep. 2021
Yep, I was looking for pretty much what Image analyst did in his code! I didn't even know sorting was a thing before today. Thanks to both of you for your answers, you have been super helpful!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 5 Sep. 2021
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
numElements = 20; % Whatever you want.
numbers = 100 * rand(1, numElements);
sortedNumbers = sort(numbers, 'descend');
theVariance = zeros(1, numElements);
for k = 1 : numElements
theVariance(k) = var(sortedNumbers(k : end));
fprintf('The variance of the first %d smallest numbers is %f.\n', ...
numElements - k + 1, theVariance(k));
end
plot(theVariance, 'b.-', 'LineWidth', 2, 'MarkerSize', 20);
xlabel('Number of Elements', 'FontSize', 20);
ylabel('Variance', 'FontSize', 20);
title('Variance as function of number of elements', 'FontSize', 20);
grid on;

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by