필터 지우기
필터 지우기

using function with loop

조회 수: 3 (최근 30일)
sermet OGUTCU
sermet OGUTCU 2021년 4월 26일
댓글: sermet OGUTCU 2021년 4월 27일
function [b, n, index] = RunLength(x)
d = [true; diff(x(:)) ~= 0]; % TRUE if values change
b = x(d); % Elements without repetitions
k = find([d.', true]); % Indices of changes
n = diff(k); % Number of repetitions
index = k(1:numel(k) - 1);
if iscolumn(x)
n = n.';
index = index.';
end
end
data = [1.2;1.0;0.05;0.4;0.3;0.2;0.1;0.05;0.04;0.03;0.03;0.02;0.01;0.001;0.01;0.01;0.001 ] ;
[b, n, index] = RunLength(data < 0.1);
match = find(b & n >= 10, 1);
result = index(match)
% This function find the number of row which 10 consecutive rows from the
% this computed row are smaller than 0.10. The result equals 8th row.
I need to use this function with loop such as;
data= 605x1 array includes numeric data
I need to compute the "result" with every 121 rows within 605 rows. So I need to run RunLength function five times (to produce five different result ) using the every 121 rows of the "data". I tried the below codes but it doesn't work;
i=1:121:605
for j=5
[b(:,j), n(:,j), index(:,j)] = RunLength(abs(data(i(j))) < 0.1);
match(j) = find(b(:,j) & n(:,j) >= 10, 1);
result(j) = index(match)
end

채택된 답변

Jan
Jan 2021년 4월 26일
편집: Jan 2021년 4월 26일
Result = zeros(1, 5);
iResult = 0;
for k = 1:121:605
datak = data(k:k+120);
[b, n, index] = RunLength(datak < 0.1);
match = find(b & n >= 10, 1);
iResult = iResult + 1;
Result(iResult) = index(match);
end
Maybe you have to catch the case, that match is empty.
Alternatively:
Result = zeros(1, 5);
iResult = 0;
for k = 1:5
index = (k - 1)*121 + 1;
datak = data(k:k+120);
[b, n, index] = RunLength(datak < 0.1);
match = find(b & n >= 10, 1);
Result(k) = index(match);
end
  댓글 수: 5
Jan
Jan 2021년 4월 27일
편집: Jan 2021년 4월 27일
"Improper assignment with rectangular empty matrix." - this is the already mentioned problem, when match is empty. In the 2nd code you'd need to insert the isempty(match) check also.
You need to advance iResult even if match is empty:
size_data=numel(data);
repitition=size_data/121;
Result = zeros(1, repitition);
iResult = 0;
for k = 1:121:size_data
datak = abs(data(k:k+120));
[b, n, index] = RunLength(datak < 0.1);
match = find(b & n >= 20, 1);
iResult = iResult + 1; % Before the IF ISEMPTY(match) check
if isempty(match)
Result(iResult) = NaN;
else
Result(iResult) = index(match);
end
end
Or slightly simplified:
size_data=numel(data);
repitition=size_data/121;
Result = NaN(1, repitition); % NaN as default
iResult = 0;
for k = 1:121:size_data
datak = abs(data(k:k+120));
[b, n, index] = RunLength(datak < 0.1);
match = find(b & n >= 20, 1);
iResult = iResult + 1;
if ~isempty(match)
Result(iResult) = index(match);
end
end
sermet OGUTCU
sermet OGUTCU 2021년 4월 27일
Dear @Jan
Thank you very much for your solution.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by