Separating a vector based on its value

2 次查看(过去 30 天)
I currently have a vector of angle data (0-360). I am trying to create a loop that will create a new vector each time a value lower than 3 occurs as my data, as it has more than on 'cycles' in it. The code I use currently is very clunky, and I am sure there is a more simple loop that could do this:
x = find(diff(CA>352));
xx = x(2:2:end);
CA1 = CA(1:(xx(1)));
CA2 = CA((xx(1)+1):(xx(2)));
CA3 = CA((xx(2)+1):(xx(3)));
CA4 = CA((xx(3)+1):(xx(4)));
CA5 = CA((xx(4)+1):(xx(5)));
Any advise or solution would be great!
  3 个评论
Jessica Smith
Jessica Smith 2016-4-29
I used this as the recorded angle didn't always hit '0' or '360', therefore, using this number seemed to identify a big enough change in angle to know a new cycle had begun. Again, admittedly very clunky
Image Analyst
Image Analyst 2016-4-29
What I did in my answer was to "create a new vector each time a value lower than 3 occurs" - did you see it. I also have no idea why 352 was used, so I used 3 like you said in the quote I just gave.

请先登录,再进行评论。

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2016-4-29
编辑:Azzi Abdelmalek 2016-4-29
v=randi([0 360],1,1000)
threshold=3
out=accumarray((cumsum(v<=threshold)+1*~(v(1)<=threshold))',1:numel(v),[],@(x) {v(x)})

更多回答(1 个)

Image Analyst
Image Analyst 2016-4-29
编辑:Image Analyst 2016-4-29
If you have the Image Processing Toolbox, you can do it (find all contiguous runs of data less than value of 3) all in just 2 lines of code.
% Create random data
data = 10 * rand(1,100)
% Now, here is the main code.
% Find places where data is less than 3
lessThan3 = data < 3;
% Get coordinates of those into new vectors.
% Each "run" of data less than 3 is in a vector called PixelValues
% that is a field of a structure array called measurements
measurements = regionprops(lessThan3, data, 'PixelValues');
% NOW WE'RE ALL DONE!
% For fun, type out all those vectors
for k = 1 : length(measurements)
fprintf('For run #%d, there are %d elements : ', k, length(measurements(k).PixelValues));
fprintf('%.3f ', measurements(k).PixelValues);
fprintf('\n');
end
Here's the data it found:
For run #1, there are 1 elements : 1.898
For run #2, there are 4 elements : 2.414 1.204 2.450 2.037
For run #3, there are 1 elements : 2.415
For run #4, there are 1 elements : 1.535
For run #5, there are 3 elements : 1.942 2.866 0.586
For run #6, there are 1 elements : 2.383
For run #7, there are 1 elements : 1.805
For run #8, there are 2 elements : 1.465 1.349
For run #9, there are 1 elements : 2.462
For run #10, there are 1 elements : 1.067
For run #11, there are 2 elements : 1.888 2.140
For run #12, there are 1 elements : 0.324
For run #13, there are 2 elements : 2.392 1.706
For run #14, there are 2 elements : 0.594 1.408
For run #15, there are 1 elements : 0.563
For run #16, there are 1 elements : 2.259
For run #17, there are 3 elements : 1.324 1.736 2.766
For run #18, there are 1 elements : 0.917
For run #19, there are 1 elements : 1.377
For run #20, there are 1 elements : 2.709
For run #21, there are 1 elements : 1.516
You can even do it all in one line of code if you want:
measurements = regionprops(data<3, data, 'PixelValues');

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by