필터 지우기
필터 지우기

How can I delete the continuously duplicated value and keep the last one in a cell array?

조회 수: 2 (최근 30일)
Hello,
I'm trying to delete the continuously duplicated value and keep the last one in a cell array. I try some ways but it does not work. Could you help me correct it?
My input data (around 4 million rows):
Symbol, Data, Time, Price, Volume
CLF7,01/03/1996,12:54:04,17.58
CLF7,01/03/1996,13:31:19,17.58
CLF7,01/03/1996,15:07:42,17.57
CLF7,01/03/1996,15:07:45,17.58
CLF7,01/03/1996,15:10:03,17.58
CLF7,01/03/1996,12:54:04,17.12
CLF7,01/03/1996,13:27:39,17.58
CLF7,02/03/1996,14:35:40,17.57
CLF7,02/03/1996,12:54:04,17.57
My required output:
Symbol, Data, Time, Price, Volume
CLF7,01/03/1996,13:31:19,17.58
CLF7,01/03/1996,15:07:42,17.57
CLF7,01/03/1996,15:10:03,17.58
CLF7,01/03/1996,12:54:04,17.12
CLF7,01/03/1996,13:27:39,17.58
CLF7,02/03/1996,12:54:04,17.57
I'm trying with 2 ways. First way, I create a new array with the same size of my data. It will find the value at price (i+1) is the same value at price (i). Then, I will copy the row have price (i+1) into the new array.
dim = size(data);
data_3 = zeros(dim);
i = 1; j = 1;
data_3(i,:) = data(i,:);
for i=2:dim(1)
if data(i,4) != data(i-1, 4)
j = j+1;
data_3(j,:) = data(i,:);
end
end
data_4 = data_3([1:j], :);
With this way, I did not any change in my data. The second way, I use "if" to find the value at a price (i) and compare with price(i-1). If the value is equal, I will delete the row (i-1)
for i=2:size(data);
If data(i,4) ~ data(i-1,4);
data(i-1,4)=[];
end
end
However, I meet the error: "Conversion to logical from struct is not possible".
I'm tried to find some similar questions in forum, but it is almost found and deleted all duplicate value compared the first appeared value and keep the last one. For my requirement, I just found and deleted the duplicated value when it is continuously duplicated value.
I'm really appreciated your help.
Thanks

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 6월 9일
f = fopen('20160609.txt');
c = textscan(f,'%s %s %s %f','delimiter',',');
fclose(f);
t =[diff(c{end})~=0;true];
C = [c{1:3}];
out = [C(t,:),num2cell(c{end}(t))];
  댓글 수: 3
Andrei Bobrov
Andrei Bobrov 2016년 6월 10일
편집: Andrei Bobrov 2016년 6월 10일
f = fopen('20160610.txt');
c = textscan(f,'%s %s %s %f %f','delimiter',',','HeaderLines',1,...
'EmptyValue',nan);
fclose(f);
C = [c{1:3}];
t = ~isnan(c{4});
C = C(t,:);
co = c{4}(t);
to =[diff(co)~=0;true];
out = [C(to,:),num2cell(co(to))]
Trung Hieu Le
Trung Hieu Le 2016년 6월 10일
Great work. Many thanks a lot for your help. I will do this code for each file. Thanks again

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Chart Technical Indicators에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by