Change element of a matrix in a row

2 views (last 30 days)
Selim Karakurt
Selim Karakurt on 3 Nov 2016
Edited: Thorsten on 4 Nov 2016
Hello community,
I am using matlab for image processing. I filter the image and get a black-white image and after that i want to count them. I finished that. The problem is that in the Matrix there are white places. For better understanding i gave you an example.
A
0 0 0 0 0
1 0 1 0 0
0 1 0 0 1
1 1 1 1 0
1 0 0 0 1
the result should be
A
0 0 0 0 0
1 1 1 0 0
0 1 1 1 1
1 1 1 1 0
1 1 1 1 1
I can't code it so please i need your help. I hope you can help me.
Thanks!
  2 Comments
Jan
Jan on 3 Nov 2016
Please explain the procedure: Do you want to the rows to be filled by 1s between the first and the lase 1?
Selim Karakurt
Selim Karakurt on 3 Nov 2016
JES! I tried to give an example. But KSSV answer was good but it dont work because i have to change my logical matrix in to a double matrix. but when i do so my double matrix is filled with only 1 and i loose my information. you can look down what i talked with him. Thanks!

Sign in to comment.

Answers (2)

KSSV
KSSV on 3 Nov 2016
Edited: KSSV on 3 Nov 2016
A = [0 0 0 0 0
1 0 1 0 0
0 1 0 0 1
1 1 1 1 0
1 0 0 0 1 ] ;
B = A ;
[m,n] = size(A) ;
for i = 1:m
idx = find(A(i,:)==1) ;
if ~isempty(idx)
B(i,idx(1):idx(end)) = 1 ;
end
end
B
  6 Comments
Selim Karakurt
Selim Karakurt on 3 Nov 2016
Edited: Selim Karakurt on 3 Nov 2016
for example something like this. all i want is to fill the picture with black.

Sign in to comment.


Thorsten
Thorsten on 4 Nov 2016
Edited: Thorsten on 4 Nov 2016
help imfill

Community Treasure Hunt

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

Start Hunting!