How to delete an item in a matrix by comparing the correlation with the next neighbor in a same column

2 views (last 30 days)
Dear all, I have faced a confusing matrix problem when I am learning image processing. Like if I have a matrix A(3*45), I would like to compare each row(1*45), if the row has a value, then get its position. Furthermore, if the following number doesn't have a value for more than 5 following steps, then stop counting the rest of the value. step Finally get the maximum (x1)and the minimum(x2) position in that row. Finally, put the answer into a matrix Q(X2, X1).Thank you !!
example:
A=[0,0,0,0,0,0,11,2,33,44,63,23,6,0,0,3,4,6,7,3,4,0,5,0,0,9,4,14,22,0,0,0,0,0,0,0,0,0,0,0,3,4,0,0,0;
0,0,4,2,4,2,57,7,4,2,7,4,6,8,4,2,4,88,4,0,4,0,0,6,0,0,0,3,4,6,2,3,5,3,2,5,6,7,0,0,0,5,6,7,22;
0,0,6,4,5,0,0,0,0,0,0,0,0,0,3,4,5,6,2,3,4,6,4,2,3,3,6,4,5,2,1,9,9,5,3,1,3,0,0,0,0,0,0,0,0,]
Desired answer:
Q=[7,29;
3,45;
15,37]
Here is my code (it's not correct):
for j = 1:size(A)
line=A(j,:);
C=find(line>=1);
x1=max(C)
x2=min(C)
Q(x1,x2)
end
  4 Comments
tengteng QQ
tengteng QQ on 13 Dec 2021
And the row no.3 is a speciall case, since the continue 0 are happen in the first few element, so it suppose will count at (3,4,5,6,2,3,4,6,4,2,3,3,6,4,5,2,1,9,9,5,3,1,3) only
Jan
Jan on 13 Dec 2021
"I would like to compare each element in the same row." - again: with what? A comparison needs two elements.
Of course, I could guess, what you want. But if you express the procedure unequivocally in English, this is a sketch for the Matlab program already.
I could guess also, that "having a value" means and value differing from 0. But guessing is not efficient for programming.
"if the following neighbor (=>5)steps continue to be 0, then stop counting the rest of the position" - following what?
I have no idea, how you achive "(3,4,5,6,2,3,4,6,4,2,3,3,6,4,5,2,1,9,9,5,3,1,3)".

Sign in to comment.

Answers (3)

Jan
Jan on 13 Dec 2021
With bold guessing:
A=[0,0,0,0,0,0,11,22,33,44,63,23,6,0,0,3,4,6,7,3,4,0,5,0,0,9,4,14,22,0,0,0,0,0,0,0,0,0,0,0,3,4,0,0,0; ...
0,0,4,2,4,2,57,7,4,2,7,4,6,8,4,2,4,88,4,0,4,0,0,6,0,0,0,3,4,6,2,3,5,3,2,5,6,7,0,0,0,5,6,7,22; ...
0,0,6,4,5,0,0,0,0,0,0,0,0,0,3,4,5,6,2,3,4,6,4,2,3,3,6,4,5,2,1,9,9,5,3,1,3,0,0,0,0,0,0,0,0];
for j = 1:size(A, 1) % Not size(A), because this replies a vector!
line = A(j, :);
blockOfZeros = strfind(line(2:end), zeros(1, 5)) + 1;
if ~isempty(blockOfZeros)
line(blockOfZeros(1):end) = 0;
end
x1 = find(line, 1, 'last');
x2 = find(line, 1, 'first');
% ??? Q(x1,x2)
% How do you want to store x1 and x2 if no matching elements
% are found?
end

tengteng QQ
tengteng QQ on 13 Dec 2021
Dear Jan, I would like to compare each element in the same row. I am trying to do step measurement.
For example : In matrix A row No.1=[0,0,0,0,0,0,11,2,33,44,63,23,6,0,0,3,4,6,7,3,4,0,5,0,0,9,4,14,22,0,0,0,0,0,0,0,0,0,0,0,3,4,0,0,0;]
it's position is (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45)
I would like to take the first position and the last position which have value in that row. Furthermore, if the following neighbor (=>5)steps continue to be 0, then stop counting the rest of the position.
So for matrix A, the answer is 11, and 22. And its position is 7,29 (final answer)
And the row no.3 is a speciall case, since the continue 0 are happen in the first few element, so it suppose will count at (3,4,5,6,2,3,4,6,4,2,3,3,6,4,5,2,1,9,9,5,3,1,3) only.
Thank you!!

Image Analyst
Image Analyst on 13 Dec 2021
Tell us what you REALLY want. Like, what are you going to do once you have the starting and ending columns at each row?
Chances are you can go line by line and use image processing functions like bwareaopen() or bwareafilt() or regionprops() to get certain regions. But I really need to know why you want these values.
And I also need to know why you want to stop counting if there are more than 5 0's in a row.
And Q can't be a rectangular array because it might be possible for you to have several runs, not exactly 2. Or do you want there to be 2? And what if there are more than 2? Just take the longest 2? If so, why?
Again we need the context for all this.
  2 Comments
tengteng QQ
tengteng QQ on 13 Dec 2021
Dear Image Analyst, I am working with the university on an image measurement project. However, I am studying chemistry and I have poor knowledge about coding.
So I have an image and I want to measure its width. And there is some noise on the edge. I would like to un-count the noise when I doing the distance measurement.
So I am trying to ask how to succeed in my thinking in the matrix.
as the image show, I only need to measure the place that has value and get the mix and min distance and compare each pixel relationship, then I can find with of the point is noise. Like the previous question, I can use this way to succeed in my measurement.
In my example, I get a row out No.1=[0,0,0,0,0,0,11,2,33,44,63,23,6,0,0,3,4,6,7,3,4,0,5,0,0,9,4,14,22,0,0,0,0,0,0,0,0,0,0,0,3,4,0,0,0;]
so when i get the position 29-7, i know that the width is 22 pixel.
==>
Image Analyst
Image Analyst on 13 Dec 2021
So what if a line crosses 2 or more white blobs? Do you want to take just the largest on that line? Or do you want to take the largest blob overall in the image? Like
mask = bwareafilt(grayImage > 128, 1);
What about holes in the blobs? Do you wan tto ignore them and just go from the far left side to the far right size of the blob? Like
[rows, columns, numColors] = size(grayImage);
mask = imfill(mask, 'holes'); % Fill any black holes in blobs.
mask = bwareafilt(mask, 1); % Take largest blob in image.
widths = zeros(rows, 1);
for row = 1 : rows
thisRow = mask(row, :);
% Take longest run, in case some part of blob curves upwards into other rows.
% This happens about 60% of the way down in your sample image.
thisRow = bwareafilt(thisRow, 1);
widths(row) = sum(thisRow)
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!