Delete items from array and split it up into new arrays
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Bjælle
am 10 Nov. 2022
Kommentiert: Image Analyst
am 10 Nov. 2022
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!
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 10 Nov. 2022
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
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')
% Identify negative indexes
mask = array < 0;
% Measure values at those indexes.
negativeRegions = regionprops('table', mask, array, 'PixelValues')
Weitere Antworten (2)
Voss
am 10 Nov. 2022
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
start_idx = strfind([true is_negative],[true false])
end_idx = strfind([is_negative true],[false true])
answer = arrayfun(@(s,e)array(s:e),start_idx,end_idx,'UniformOutput',false)
2 Kommentare
AH
am 10 Nov. 2022
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]'
% 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
eidx = find(diff(binMask) == -1)
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
You can also achieve this task using the functions sigrangebinmask and binmask2sigroi.
3 Kommentare
Image Analyst
am 10 Nov. 2022
@AH, what toolbox is sigrangebinmask in? It's not in the Signal Processing Toolbox.
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!