how to add ones to binary matrix with condition to add them ?
1 view (last 30 days)
Show older comments
Firas Al-Kharabsheh
on 5 Apr 2016
Edited: Andrei Bobrov
on 6 Apr 2016
if i have a four matrix like this
A = [ 1 0 0 1 1 1
0 1 0 1 0 0
1 0 1 0 0 1
0 1 0 0 1 1 ]
AND matrix B represent number of group of one's each row
B = [ 1 3 0
1 1 0
1 1 1
1 2 0 ]
AND matrix C like this
C = [ 0 0 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 1
0 0 0 0 0 1 ]
If i return the last column in C then i need function to determine if there exists a 1 in last row
if yes then return to the same row in Matrix B and put number of ones equal to the last number nonzero in B
for example
last column in C = [ 1
0
1
1 ]
then there is 1 in the first row >> then go back to first row in matrix B AND find last number nonzero in the first row which is equal to 3
the put in matrix C THE THREE ONE LIKE THIS
D = [ 0 0 0 1 1 1
0 0 0 0 0 0
0 0 0 0 0 1
0 0 0 0 0 1 ]
and the same thing will be happened to last row ,, the final solution will be
D =[ 0 0 0 1 1 1
0 0 0 0 0 0
0 0 0 0 0 1
0 0 0 0 1 1 ]
2 Comments
Azzi Abdelmalek
on 5 Apr 2016
If i return the last column in C then i need function to determine if there exists a 1 in last row
Are you sur you don't mean in the last column?
How A is involved in your question?
Image Analyst
on 5 Apr 2016
Is any different than this question or this one or this one or this one or numerous other copies? Why don't you just give us the larger context so we know what you want to do instead of giving us complicated, arcane rules? For example in one of your questions I showed you how imreconstruct() would give the desired result in one line of code.
Accepted Answer
Azzi Abdelmalek
on 5 Apr 2016
B = [ 1 3 0
1 1 0
1 1 1
1 2 0 ]
C = [ 0 0 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 1
0 0 0 0 0 1 ]
n=find(C(:,end));
D=zeros(size(C))
kk=0
for k=1:numel(n)
b=nonzeros(B(n(k),:));
m=b(end)
D(n(k),end-m+1:end)=ones(1,m)
end
3 Comments
More Answers (1)
Andrei Bobrov
on 5 Apr 2016
Edited: Andrei Bobrov
on 6 Apr 2016
A1 = fliplr(A);
D = bsxfun(@times,C(:,end),fliplr(cumsum([A1(:,1),diff(A1,1,2)==1],2).*A1)) == 1;
D = bsxfun(@and,C(:,1),cumsum([A(:,1), diff(A,1,2) == 1],2).*A==1)
0 Comments
See Also
Categories
Find more on Logical 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!