Extract a portion of vector
11 views (last 30 days)
Show older comments
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
Accepted Answer
More Answers (3)
Matt Fig
on 22 Oct 2012
Edited: Matt Fig
on 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 Comments
Matt Fig
on 22 Oct 2012
Edited: Matt Fig
on 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."
Jonathan Epperl
on 22 Oct 2012
How about
vector(vector(:,1)<20, 2) = NaN;
Logical indexing...
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!