Extract a portion of vector

9 vues (au cours des 30 derniers jours)
Alessandro Antonini
Alessandro Antonini le 22 Oct 2012
Hi, I have a problem that I can not solve.
I need to extract a portion of my vector, this vector has 2 columns (first column is time and second is wave height) and 2500 rows, the values of the second column are number and NaN, the data are sampled every 3 hours. The problem is: I need to create a new vector that contains only the registration longer than 20 hours, and for the rest I must to replace with NaN.
Here an example:
Initial vector= [0 0.2
3 0.35
6 NaN
9 NaN
12 NaN
15 NaN
18 NaN
21 NaN
24 0.29
27 0
30 0
33 0
36 0
39 0
42 0
45 0.18
48 0.33
51 NaN
54 NaN
57 NaN]
The result should be
[0 NaN
3 NaN
6 NaN
9 NaN
12 NaN
15 NaN
18 NaN
21 NaN
24 0.29
27 0
30 0
33 0
36 0
39 0
42 0
45 0.18
48 0.33
51 NaN
54 NaN
57 NaN]
Can anyone suggest an approach to solve this problem?
Thanking you in advance
Alessandro Antonini
  3 commentaires
Alessandro Antonini
Alessandro Antonini le 22 Oct 2012
No, * doesn't mean anything. I tried to write in bold the first two rows of the vector and * it appeared. It is wrong, there is not any *.
Walter Roberson
Walter Roberson le 22 Oct 2012

Connectez-vous pour commenter.

Réponse acceptée

Alessandro Antonini
Alessandro Antonini le 22 Oct 2012
I solve the problem with the help of my friend, I needed two while cicle.
to solution was:
C=impuct vector and e1=C(:,2), the code is:
i=1;
while i<length(e1)
j=0;
i
while isnan(e1(i+j))==0 && (i+j)<length(e1)
j=j+1;
end
if j<7
e1(i:(i+j-1))=NaN;
end
i=i+j+1;
end

Plus de réponses (3)

Matt Fig
Matt Fig le 22 Oct 2012
Modifié(e) : Matt Fig le 22 Oct 2012
Say this is your vector:
A = [3 4;5 6;8 7;9 8;7 5;5 3;3 9;12 4];
And you want all rows from 5 to the end to be nan:
A(5:end,:) = nan;
  2 commentaires
Matt Fig
Matt Fig le 22 Oct 2012
Modifié(e) : Matt Fig le 22 Oct 2012
Alessandro comments:
"no, I think is different. This is my imput: A=[0 0.3; 3 23; 6 NaN; 9 NaN; 12 NaN; 15 NaN; 18 NaN; 21 NaN; 24 0.29; 27 0; 30 0; 33 0; 36 0; 39 0; 42 0; 45 0.18; 48 0.33; 51 NaN;]
The output should be: B=[0 NaN; 3 NaN; 6 NaN; 9 NaN; 12 NaN; 15 NaN; 18 NaN; 21 NaN; 24 0.29; 27 0; 30 0; 33 0; 36 0; 39 0; 42 0; 45 0.18; 48 0.33; 51 NaN;] In which, the position (1,2) and (2,2), becomes NaN, because I need to take into account only the registration more long than 20 hours. The hours are in the first column. As it can be noticed in the vector B, from position (9,2) till (17,2) the values are the same than in vector A, because the registration is longer than 20 hours. I am sorry for my poor exposition, I hope that this can help to understand my problem."
Matt Fig
Matt Fig le 22 Oct 2012
Modifié(e) : Matt Fig le 22 Oct 2012
So you simply use the same concept....
B = A; % Make a copy
B(B(:,1)<=20,2) = nan

Connectez-vous pour commenter.


Alessandro Antonini
Alessandro Antonini le 22 Oct 2012
Modifié(e) : Alessandro Antonini le 22 Oct 2012
I need to take into account only the partial measure more long than 20 hours, that is if I have a vector like that:
A=[0 2;
3 NaN;
6 3;
9 NaN;
12 4;
15 NaN;
18 5;
21 NaN;
24 6;
27 NaN;
30 7;
33 NaN;
36 8;
39 NaN;
42 9;
45 NaN;
48 10;
51 NaN;]
the result will be:
A=[0 NaN;
3 NaN;
6 NaN;
9 NaN;
12 NaN;
15 NaN;
18 NaN;
21 NaN;
24 NaN;
27 NaN;
30 NaN;
33 NaN;
36 NaN;
39 NaN;
42 NaN;
45 NaN;
48 NaN;
51 NaN;]
I need to consider only if the partial and continues duration is more long than 20 hours, as in the sequent vector from row n°4 to row n° 11, (partial duration of the registration (C(1,11)-C(1,4))=30-9=21 hours), only in this case I need to take into account. All the other values registered I don't want to take into account, they should be replaced with NaN
C=[0 0.3
3 23
6 NaN
9 0.2
12 0.53
15 0.7
18 0.1
21 0.02
24 0.53
27 0.18
30 0.25
33 NaN
36 NaN
39 NaN
42 NaN
45 NaN
48 0.33
51 NaN]
If the vector C is the imput, the result of the operation should be (vector D):
D=[0 NaN;
3 NaN;
6 NaN;
9 0.2;
12 0.53;
15 0.7;
18 0.1;
21 0.02;
24 0.53;
27 0.18;
30 0.25;
33 NaN;
36 NaN;
39 NaN;
42 NaN;
45 NaN;
48 NaN;
51 NaN];
In which, the values in the position D(1,2) and D(2,2) now are NaN, because the partial duration of the registration (continues and different from NaN)is lower than 20 hours, i.e. C(2,1)-C(1,1)=3-0=3.
At the end, the goal would be, take into account only those values registered that have a continuous registration more long than 20 hours.

Jonathan Epperl
Jonathan Epperl le 22 Oct 2012
How about
vector(vector(:,1)<20, 2) = NaN;
Logical indexing...

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by