Clear Filters
Clear Filters

How to generate sequences from matrix

3 views (last 30 days)
Ju Lien
Ju Lien on 18 Sep 2016
Edited: test run on 25 Sep 2016
Hi,
I'm very new on Matlab and i have a problem with an exercise. I have this matrix :
The first column represent the time in secondes and the second column represent the data measured. The data should be measured each 1 secondes, but sometimes lines of data are not measured in a correct time (like 2.1 and 5.8). So I have to write a procedure which find sequences of data measured in the correct time and put it in a table. Example:
With this matrix we have 3 good sequences :
1
3, 4, 5
7, 8, 9, 10
So the table should be :
Edit - This is what I did until now :
V = [1.0000 2.1000 3.0000 4.0000 5.0000 5.8000 7.0000 8.0000 9.0000 10.0000];
r=find(V(2:length(V))-V(1:length(V)-1)~=1);
V(r(2:2:end))=[]
So i could identify the wrong values and deleted it, but now my problem is to make the table i showed before.
Edit - To make the table, i tried something like this :
V=[1.0000 3.0000 4.0000 5.0000 7.0000 8.0000 9.0000 10.0000];
r=find(diff(V)~=1)
S1=V(1:r(1))
S2=V(r(1)+1:r(2))
S3=V(r(2)+1:end)
Now i can separated each sequences of the vector, but i can't find a solution to organise it in a table.
  2 Comments
John D'Errico
John D'Errico on 19 Sep 2016
Edited: John D'Errico on 19 Sep 2016
So why not make an effort? Do some reading. I'll give you a hint: you probably want to do interpolation, so interp1 might help you. When you actually have a question about MATLAB, then ask it. But make an effort. Show what you have tried, and what went wrong.
José-Luis
José-Luis on 19 Sep 2016
You might want to read about logical indexing.
Hint:
isInt = value == floor(value)

Sign in to comment.

Accepted Answer

test run
test run on 19 Sep 2016
Hi, I understand, not allways reading the help of matlab is understandable.
You could take only the good data by doing like this:
V = [1.0000 2.1000 3.0000 4.0000 5.0000 5.8000 7.0000 8.0000 9.0000 10.0000];
s = find(floor(V)==V);
V = V(s);
Now I'll explain a bit:
floor(V)
gives you a vector that contains the numbers that were in V only that they were rounded towards minus infinity , for example 2.1 will turn into 2 , 5.8 will turn into 5.
find(floor(V)==V)
will give you a vector that returns the indexes of the terms of V which comply with the condition
floor(V)==V
meaning the indexes of the numbers that are integers, so that the vector
s
is a vector that contains the indexes of the terms that are integers. and
V=V(s);
will give you the vector V with only the terms that are in the indexes s.
Good luck
  8 Comments
Ju Lien
Ju Lien on 22 Sep 2016
Ok thank you :) Because I don't have idea how to realize that ...
test run
test run on 24 Sep 2016
Edited: test run on 25 Sep 2016
This code should do what you're looking for:
V = [1.0000 2.1000 3.0000 4.0000 5.0000 5.8000 7.0000 8.0000 9.0000 10.0000];
s = find(floor(V)==V);
V2 = V;
V = V(s);
d = length(V2);
sequence = 1:d;
helpseq = zeros(1,d);
count = 0;
for i=1:d
if rem(V2(i),floor(V2(i)))==0
count = count + 1;
if i==d
helpseq(count) = helpseq(count)+1;
break
end
else
helpseq(count) = helpseq(count)+1;
count = 0;
end
end
table(sequence',helpseq')
This should do what you wanted to achieve. You can try it with this code to test it:
V = [1.0000 2.1000 3.0000 4.0000 5.0000 5.8000 7.0000 8.0000 9.0000 10.0000 11.1 12 13 14.3 15 16 17 18.2 19];
s = find(floor(V)==V);
V2 = V;
V = V(s);
d = length(V2);
sequence = 1:d;
helpseq = zeros(1,d);
count = 0;
for i=1:d
if rem(V2(i),floor(V2(i)))==0
count = count + 1;
if i==d
helpseq(count) = helpseq(count)+1;
break
end
else
helpseq(count) = helpseq(count)+1;
count = 0;
end
end
%This is to clean up the vectors sequence and helpseq from unnecesary information.
s2 = find(helpseq ~= 0);
s2 = max(s2);
helpseq = helpseq(1:s2);
sequence = sequence(1:s2);
clear i d count s2 V2
%cleaning done.
table(sequence',helpseq')
Just one more thing, please don't leave this answer unnacepted if it actually did answer to your question.
Also if you want explanations about the code I wrote add a comment asking for an explanation to the code.
Good luck.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!