indexing using for loop
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Akira Agata
on 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
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!