indexing using for loop

조회 수: 2 (최근 30일)
Margaret Winding
Margaret Winding 2017년 3월 10일
답변: Akira Agata 2017년 3월 10일
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.

답변 (1개)

Akira Agata
Akira Agata 2017년 3월 10일
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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by