Index of value exceeding threshold for each row

조회 수: 12 (최근 30일)
Boris
Boris 2020년 1월 30일
답변: Vinai Datta Thatiparthi 2020년 2월 3일
I got this problem:
Lets say I have this matrix [ 1 4 7 19 23 60 79 81 100 90 57 43 , 2 5 7 20 51 77 84 90 101 105 88 56, ...]
I need to find the index of the first value in each row exceeding the threshold of 80.
So for the first row it will be 8 , the second row will be 7, etc.
My matrix consist of 144 columns and 80000 rows. So my output will be a single column with 80000 rows.

답변 (1개)

Vinai Datta Thatiparthi
Vinai Datta Thatiparthi 2020년 2월 3일
Hello Boris,
This simple approach could solve the problem -
values = [ 1 4 7 19 23 60 79 81 100 90 57 43; 2 5 7 20 51 77 84 90 101 105 88 56]; %Your input values
% Note: In MATLAB, rows in matrices are seperated by a semicolon symbol and not the comma symbol
indices = zeros(size(values, 1), 1); %Array to hold the final outputs
threshold = 80; %Threshold value
for i=1:size(values, 1)
greaterThan = find(values(i,:) > threshold); %find function returns array of indices of ...
% all the values that are greater than the threshold
indices(i) = greaterThan(1); %We only need the first such index
end
Hope this helps!

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by