how to add ones to binary matrix with condition to add them ?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Azzi Abdelmalek
2016-4-5
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
2016-4-5
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.
采纳的回答
Azzi Abdelmalek
2016-4-5
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 个评论
更多回答(1 个)
Andrei Bobrov
2016-4-5
编辑:Andrei Bobrov
2016-4-6
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!