how to add ones to binary matrix with condition to add them ?
2 ビュー (過去 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 で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!