Copy part of an cell array(double) into a new column next to it

1 view (last 30 days)
Hey
so i have some data in a column which is of type double. an example:
V
___
231
234
232
230
229
... etc.
so what i want to do is, to make the matlab sort the values over 230 into a new column so it looks like this: _ _ 231 231 234 234 232 232 230 0 229 0 ... etc.
i Wrote this code: V(:,2) = V(:,1) > 230; i obtained the result, but in the second column i get 1 on the places with values over 230 and 0 in the places with values lower than 230..
Suggestion of why and what i can do?.. this only works when the Cell is double...if i change it to string..it will display error and say something about 'gt' etc.
thanks in advance

Answers (1)

Kelly Kearney
Kelly Kearney on 18 Mar 2014
V = [...
231
234
232
230
229];
V(:,2) = 0;
V(V(:,1)>230,2) = V(V(:,1)>230,1)
Results:
V =
231 231
234 234
232 232
230 0
229 0
Not sure what you mean about the string... you can only do numerical comparisons on numbers (double, int, logical, etc), not strings, so the error makes sense. If your data begins as a cell array of strings, you'll have to convert it first.
  2 Comments
awda
awda on 18 Mar 2014
thanks alot this was helpful. :D...but if i want the second cell to not show the Zeros. which will make the next number over 230 to be under the other one. so the spaces between 231 0 232 for example is gone and it shows as 231 232 and so on.
Kelly Kearney
Kelly Kearney on 18 Mar 2014
Not at all clear what you mean... how about an example?
(And make sure you format your post; right now I'm not sure if you really want row-vector output or if you're just failing to format what you're typing.)

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!