How can I sorting the values in the jumping columns?
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
Guillaume
on 13 Feb 2015
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?
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
More Answers (0)
Categories
Find more on Shifting and Sorting Matrices 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!