indexing using for loop

2 visualizaciones (últimos 30 días)
Margaret Winding
Margaret Winding el 10 de Mzo. de 2017
Respondida: Akira Agata el 10 de Mzo. de 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.

Respuestas (1)

Akira Agata
Akira Agata el 10 de Mzo. de 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

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by