How can I sorting the values in the jumping columns?

I have a matrices 25x600 and some columns contains positive and negative values. I want to the output like this [+ + - -] (four values 2 positive and 2 negative). I am guaranteed to always have two positive values immediately before the transition and two negative values immediately after. my attempting was as follow :
clc;
clear all;
close all;
data=[-0.0059972;-0.004994;-0.0029881;2.0868e-05;0.0030299;0.013059;0.033115;0.063196;0.093273;0.1935;0.39385;0.69423;0.99448;1.9950;3.99550;6.99550;9.9957;19.9961;39.99620;69.9960;99.99530;199.99810;399.99140;699.98860;1000.03130]
for r=1:600
lam=data(:,r);
N_lam = length(lam);
for j=1:N_lam
kk=0;
r1=0;
if(sign(lam(j))==1)
kk=kk+1;
lampos(kk)=lam(j);
if (length(lampos(kk))>2 &length(lamneg(r1))>2)
break
end
else
r1=r1+1;
lamneg(r1)=lam(j);
end
end
cc{r}=[lampos lamneg];
end
Any help would be greatly appreciated. I will be grateful to you

3 Comments

What does pick four values mean, what do you want to return? Rather than posting code that's not valid (matlab does not have goto), post an example of input and wanted output.
In addition, are you guaranteed to always have two positive values immediately before the transition and two negative values immediately after?
Ahmed
Ahmed on 13 Feb 2015
Edited: Ahmed on 13 Feb 2015
Sorry but I run the code and I got one value positive and one negative but not as I want. Yes, I am guaranteed to always have two positive values immediately before the transition and two negative values immediately after
First of all, you can't have a matrix like [+ + - -] - the best you can do is [1,1,-1,-1]. Or you could have any other values that have those signs I guess. But if that's it, then where, from an arbitrary matrix of a bunch of values, get those four values? You said "I want to the output like this [+ + - -] (four values 2 positive and 2 negative)." so which of the 15,000 elements would you extract to stick into the 4 element output array? Would I just pull 4 at random? I can't figure out what your for loop is doing - perhaps you can explain it in words. And it looks like it's looping over all elements so you will probably get more than 4 output values.

Sign in to comment.

 Accepted Answer

You haven't answered my question about what you want as an output. Possibly this:
%note that your example data doesn't have any transition from + to -
%some other example data:
data = [1 2 3 4 -5 -6 -7 -8 9 10 -11 -12 -13 -14];
postoneg = find(diff(sign(data)) == -2)
transitions = data([postoneg-1; postoneg; postoneg+1; postoneg+2])

3 Comments

Thanks, I want the output as array contains 4 point (2 negative 2 positive) For example for the first column I have
data(25,1)=[
-0.0006662
-0.00033225
0.00033638
0.001341
0.0023478
0.0057178
0.012524
0.022899
0.033476
0.070152
0.15002
0.28558
0.43904
1.0598
2.6344
5.3640
8.2462
18.1135
38.0530
68.0307
98.0210
198.0086
397.9984
698.0106
998.0148]
I want the out put
transitions=[ -0.0006662 -0.00033225 0.00033638 0.001341]
I tried to use your code put I got empty array
Of course, you've got an empty array. You wrote I am guaranteed to always have two positive [...] before the transition and two negative [...] after. Your example is the exact opposite.
So, the question becomes, do you want positive to negative transitions as you've stated:
transitionindex = find(diff(sign(data)) == -2);
Do you want negative to positive transition as in your example:
transitionindex = find(diff(sign(data)) == 2);
Or both:
transitionindex = find(abs(diff(sign(data))) == 2);
Note that my answer applied to a row vector (as in my example). For a column vector, you need to concatenate the transition offsets horizontally instead of vertically (commas or spaces instead of semi-colons) ie:
transitions = data([transitionindex-1, transitionindex, transitionindex+1, transitionindex+2]);
Your question originally mention a matrix. You'll have to adapt the code for that as you've never explained what the output should be in that case.
Thank you so much for your help

Sign in to comment.

More Answers (0)

Categories

Tags

No tags entered yet.

Asked:

on 13 Feb 2015

Commented:

on 14 Feb 2015

Community Treasure Hunt

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

Start Hunting!