Delete items from array and split it up into new arrays
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Image Analyst
2022-11-10
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')
更多回答(2 个)
Voss
2022-11-10
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)
AH
2022-11-10
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 个评论
Image Analyst
2022-11-10
@AH, what toolbox is sigrangebinmask in? It's not in the Signal Processing Toolbox.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!