How to generate sequences from matrix

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 comentarios

John D'Errico
John D'Errico el 19 de Sept. de 2016
Editada: John D'Errico el 19 de Sept. de 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.
You might want to read about logical indexing.
Hint:
isInt = value == floor(value)

Iniciar sesión para comentar.

 Respuesta aceptada

test run
test run el 19 de Sept. de 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 comentarios

Ju Lien
Ju Lien el 19 de Sept. de 2016
Ok thanks a lot for this solution and the explanation, indeed it's very simpler than mine. So now with this new vector, do you know if it's possible to organise the values in a table like I showed in the topic?
José-Luis
José-Luis el 19 de Sept. de 2016
You could use a table
test run
test run el 19 de Sept. de 2016
Editada: test run el 19 de Sept. de 2016
What do you mean by table? Can you explain to me better what you want to do exactly?? what Jose Luis said might be the solution to make the table. Try this and let me know if that is what you want:
V = [1.0000 2.1000 3.0000 4.0000 5.0000 5.8000 7.0000 8.0000 9.0000 10.0000];
results = [0.841471 0.909297 0.14112 -0.7568 -0.95892 -0.27942 0.656987 0.989358 0.412118 -0.54402];
s = find(floor(V)==V);
V = V(s);
results = results(s);
V=V';
results=results';
table(V,results)
This will show a table in the matlab workspace, is this what you were looking for?
José-Luis
José-Luis el 20 de Sept. de 2016
Click on the link in my previous comment to see what I mean.
Ju Lien
Ju Lien el 20 de Sept. de 2016
When i say table i talk about something like that :
I want to organise each sequences of this vector by length. For example with the vector V=[1.0000 3.0000 4.0000 5.0000 7.0000 8.0000 9.0000 10.0000]; we have :
- One sequence of length 1 (--> 1)
- Zero sequence of length 2
- One sequence of length 3 (--> 3, 4, 5)
- One sequence of length 4 (--> 7, 8, 9, 10)
- Zero sequence of lenth 5
Here it's just an example with this small vector, but it could be a longer one and we could have I don't know like 3 sequences of length 3, 2 sequences of length 2 etc...
I'd like to organise my table like this, I hope you understand what I mean... So until now i know how to seperate each sequences of the vector but I don't know how to build the table and organise it like I just explained.
test run
test run el 21 de Sept. de 2016
I understand now what you want to do. Let me think about it.
Ju Lien
Ju Lien el 22 de Sept. de 2016
Ok thank you :) Because I don't have idea how to realize that ...
test run
test run el 24 de Sept. de 2016
Editada: test run el 25 de Sept. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 18 de Sept. de 2016

Editada:

el 25 de Sept. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by