Calculate the mean of cells next to each other in an Array where value is smaller than 1
1 view (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
Image Analyst
on 18 Dec 2016
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 Comments
Image Analyst
on 20 Dec 2016
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
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!