Calculate the mean of cells next to each other in an Array where value is smaller than 1

1 次查看(过去 30 天)
Hello Everybody,
I have got an array, where I want to identify values smaller than one. After this I need to calculate the mean of those cells in the other rows af the array and use just the single mean-value as new value for the previous dataset.
Example:
Here, the second row needs to be analysed for every value smaller 1, the values of row one needs to be replaced by just one value which is the mean value of the replaced ones. So mean of column 126 and 127 and next mean of column 129 and 130 and so on. The two rows will be replaced by the new values.
Do you got me and could think of any solution for this
Thanks a lot!

回答(1 个)

Image Analyst
Image Analyst 2016-12-18
Why don't you (1) supply us with data we can use to try things, and (2) explain it better.
Like explain this contradiction "the values of row one needs to be replaced" and "The two rows will be replaced". Because of wording like that, I have no idea what rows and/or columns need to be replaced and with what. But I'm thinking that conv() to get a sliding mean of two variables, or blockproc() if you want the window of 2 elements to move along in "jumps" of two elements, might be part of the solution.
  7 个评论
J.P.
J.P. 2016-12-20
Hey! First of all. I am really thankfull for the fast responses.
1)The script does work, but it doesn't really gives the mean. For the X-Data it gives 185.77 instead of (0.18+0.072)/2=0.126 for the first two identified values.
2) I need the values which are identified as <1 replaced by just one mean-value, and your script gives again each point.
Probably you have time and like to do some more. I try to understand what you did and maybe fix it on my own.
Thank you very much so far.
Image Analyst
Image Analyst 2016-12-20
Sorry - I misunderstood. Try this:
% Define row 2
X = [6.9840 7.0920 7.2000 7.2360 7.2000 7.1280 0.1800 0.0720 3.9960 0.1080 0.0720 2.4480 0.1800 0.1800 0.1800 1.0080 0.6840 0.1080 0.0720 0.0720 0.5040]
% Define row 1
Y = [185.4600 185.4700 185.5200 185.5500 185.6500 185.7200 185.7700 185.7800 185.8400 185.8600 185.8600 185.8400 185.7800 185.7800 185.7900 185.5300 185.4900 185.4800 185.4800 185.4600 185.4600]
% Y is the location - I guess???
% Initialize output values
outX = X;
outY = Y;
% Find where row 2 < 1 -- find the "blobs"
binaryData = X < 1;
% Label the data. Give each blob an ID number/label.
[labeledData, numRegions] = bwlabel(binaryData);
% Measure the mean values of Y in the blob, and find the locations
props = regionprops(labeledData, Y, 'PixelList');
% Replace X and Y with the mean values
for k = 1 : numRegions
% Find out where each blob resides.
theseIndexes = props(k).PixelList(:, 1);
% Replace Y (locations) with mean Y of this blob.
outY(theseIndexes) = mean(Y(theseIndexes));
% Replace X with mean X value of this blob.
outX(theseIndexes) = mean(X(theseIndexes));
end
% Remove duplicates
indexesToRemove = [];
for k = 1 : numRegions
% Find out where each blob resides.
theseIndexes = props(k).PixelList(:, 1);
if length(theseIndexes) == 1
continue; % Skip single values
end
indexesToRemove = [indexesToRemove; theseIndexes(2:end)];
end
% Now that we've found the duplicate indexes, remove them.
outY(indexesToRemove) = [];
outX(indexesToRemove) = [];
% Print to command window
outY
outX

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by