How to compute the variance of an array progressively taking away the highest values?
28 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Happy_Shovel
am 5 Sep. 2021
Kommentiert: Happy_Shovel
am 5 Sep. 2021
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
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.
Akzeptierte Antwort
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)
Siehe auch
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!