Run length encoding error

조회 수: 1 (최근 30일)
Dmitrij Linivenko
Dmitrij Linivenko 2020년 9월 15일
댓글: Ameer Hamza 2020년 9월 16일
Hi,
I'm trying to do RLE encoding using data sheet with temeperature values, but it gives me an error : "Index in position 1 is invalid. Array indices must be positive integers or logical values" for line 15. Can't find the solution for this.
clear all
clc
data = importdata('Temperature.mat');
j = 1;
elems = zeros(length(data),1); % store repeated elements
for i = 1:length(data)
if isempty(find(elems == data(i)))
elems(j) = data(i);
j = j + 1;
end
end
elems
t = tabulate(data); % get frequency table
for n = 1:length(elems);
lens(n) = t(elems(n),1); % get how many each element in elems vector occurs
end
lens

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 9월 15일
I didn't understand the logic of your code, but here is an alternate solution.
clear all
clc
data = importdata('Temperature.mat');
grps = cumsum([0; diff(data)~=0])+1;
parts = splitapply(@(x) {x}, data, grps);
rle = cellfun(@(x) [x(1) numel(x)], parts, 'uni', 0);
rle = vertcat(rle{:});
First column is values and second column is counts.
  댓글 수: 2
Dmitrij Linivenko
Dmitrij Linivenko 2020년 9월 16일
Thanks. Now I just need to figure out what those unknown functions do as i've never seen them :D
Ameer Hamza
Ameer Hamza 2020년 9월 16일
I am glad to be of help! You can run each line one by one to get some idea which each step is doing. Documentation will also be helpful.

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

추가 답변 (0개)

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by