Delete items from array and split it up into new arrays

Hi,
I have a long array consisting of a long section of negative values, then a long section of positive values, then a long section of negative values and so on. I would like to remove all the negative values from my array, but at the same time split up the array into the individual long sections of positive values.
Simplified example:
array = [-0.0016 -0.0017 0.1746 0.1834 -0.0017 -0.0018 0.1567 0.1456 0.1744 -0.0018 -0.0017 0.1243 0.1456]
And then I would like this:
answer = {0.1746 0.1834} {0.1567 0.1456 0.1744} {0.1243 0.1456}
So the negative values has been removed, and the array is split up into new arrays containing the positive values from each section.
I hope it makes sense!

 Réponse acceptée

You can easily do it if you have the Image Processing Toolbox.
array = [-0.0016 -0.0017 0.1746 0.1834 -0.0017 -0.0018 0.1567 0.1456 0.1744 -0.0018 -0.0017 0.1243 0.1456];
% Identify positive indexes
mask = array >= 0;
% Measure values at those indexes.
positiveRegions = regionprops(mask, array, 'PixelValues');
% Identify negative indexes
mask = array < 0;
% Measure values at those indexes.
negativeRegions = regionprops(mask, array, 'PixelValues');
positiveRegions and negativeRegions are structure arrays (rather than cell arrays). To extract the numbers for a particular region, say the 2nd region you can do
values = positiveRegions(2).PixelValues
values = 1×3
0.1567 0.1456 0.1744
You can also get them in a table if you want:
array = [-0.0016 -0.0017 0.1746 0.1834 -0.0017 -0.0018 0.1567 0.1456 0.1744 -0.0018 -0.0017 0.1243 0.1456];
% Identify positive indexes
mask = array >= 0;
% Measure values at those indexes.
positiveRegions = regionprops('table', mask, array, 'PixelValues')
positiveRegions = 3×1 table
PixelValues ________________________ {[ 0.1746 0.1834]} {[0.1567 0.1456 0.1744]} {[ 0.1243 0.1456]}
% Identify negative indexes
mask = array < 0;
% Measure values at those indexes.
negativeRegions = regionprops('table', mask, array, 'PixelValues')
negativeRegions = 3×1 table
PixelValues __________________ -0.0016 -0.0017 -0.0017 -0.0018 -0.0018 -0.0017

Plus de réponses (2)

array = [-0.0016 -0.0017 0.1746 0.1834 -0.0017 -0.0018 0.1567 0.1456 0.1744 -0.0018 -0.0017 0.1243 0.1456];
is_negative = array < 0
is_negative = 1×13 logical array
1 1 0 0 1 1 0 0 0 1 1 0 0
start_idx = strfind([true is_negative],[true false])
start_idx = 1×3
3 7 12
end_idx = strfind([is_negative true],[false true])
end_idx = 1×3
4 9 13
answer = arrayfun(@(s,e)array(s:e),start_idx,end_idx,'UniformOutput',false)
answer = 1×3 cell array
{[0.1746 0.1834]} {[0.1567 0.1456 0.1744]} {[0.1243 0.1456]}

2 commentaires

Thank you so much!
You're welcome!

Connectez-vous pour commenter.

You may find the code below doing the desired task
array = [-0.0016 -0.0017 0.1746 0.1834 -0.0017 -0.0018 0.1567 0.1456 0.1744 -0.0018 -0.0017 0.1243 0.1456]'
array = 13×1
-0.0016 -0.0017 0.1746 0.1834 -0.0017 -0.0018 0.1567 0.1456 0.1744 -0.0018
% find the mask where the array is non-negative
binMask = array >= 0;
% Find the start and end index of each region of interest
sidx = find(diff(binMask) == 1)+1
sidx = 3×1
3 7 12
eidx = find(diff(binMask) == -1)
eidx = 2×1
4 9
if (binMask(1) == 1)
sidx = [1;sidx];
end
if (binMask(end) == 1)
eidx = [eidx; length(array)];
end
% Split it into cells
ncell = min(length(sidx),length(eidx));
A = cell(ncell,1);
for i = 1:ncell
A{i} = array(sidx(i):eidx(i));
end
A
A = 3×1 cell array
{2×1 double} {3×1 double} {2×1 double}
You can also achieve this task using the functions sigrangebinmask and binmask2sigroi.

3 commentaires

Here is the code using sigrangebinmask and binmask2sigroi
% Find mask
threshold = 0; % split array into negative and non-negative parts
l = sigrangebinmask(array,threshold)
roilims = binmask2sigroi(l)
sidx = roilims(:,1) % beginning index
eidx = roilims(:,2) % ending index
ncel = size(roilims,1)
Thank you very much!
@AH, what toolbox is sigrangebinmask in? It's not in the Signal Processing Toolbox.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by