indexing using for loop

2 Ansichten (letzte 30 Tage)
Margaret Winding
Margaret Winding am 10 Mär. 2017
Beantwortet: Akira Agata am 10 Mär. 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.

Antworten (1)

Akira Agata
Akira Agata am 10 Mär. 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

Kategorien

Mehr zu Creating and Concatenating Matrices 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