indexing using for loop

2 vues (au cours des 30 derniers jours)
Margaret Winding
Margaret Winding le 10 Mar 2017
I have a column vector with 480 values. I am asked to do the following:
Find the number of singular values required to capture at least 90% of the variance, i.e. the smallest index such that the value in the vector is greater than 0.90.
I don't really understand what this means/how to do it. I was told that you can somehow use a for loop and if statement to compute this, but I don't even understand what this is asking and how to go about that.

Réponses (1)

Akira Agata
Akira Agata le 10 Mar 2017
Of course you can do it with using for loop and if statement. But MATLAB can do it much better way like this:
x = rand(480, 1);
x = sort(x); % column vector with 480 values
idx = find(x > 0.9, 1); % smallest index such that x > 0.9
If you want to use for loop and if statement, the code becomes much longer, such as:
x = rand(480, 1);
x = sort(x); % column vector with 480 values
for kk = 1:480
if x(kk) > 0.9
idx = kk; % smallest index such that x > 0.9
break;
end
end

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by