How can I sorting the values in the jumping columns?
2 views (last 30 days)
Show older comments
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
Image Analyst
on 13 Feb 2015
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.
Accepted Answer
Guillaume
on 13 Feb 2015
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
Guillaume
on 14 Feb 2015
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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!