フィルターのクリア

how to add ones to binary matrix with condition to add them ?

3 ビュー (過去 30 日間)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016 年 4 月 5 日
編集済み: Andrei Bobrov 2016 年 4 月 6 日
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
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
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
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 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 5 日
The first column of what?
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016 年 4 月 5 日
If
A = [ 1 1 0 0 1
1 1 1 0 0
0 0 1 0 0
1 0 0 1 1 ]
and B = [ 2 1
3 0
1 0
1 2 ]
and C = [ 1 0 0 0 0
1 0 0 0 0
0 0 0 0 0
1 0 0 0 0 ]
THEN how to apply to be D like that
D = [ 1 1 0 0 0
1 1 1 0 0
0 0 0 0 0
1 0 0 0 0 ]

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
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;
on last comment by Firas
D = bsxfun(@and,C(:,1),cumsum([A(:,1), diff(A,1,2) == 1],2).*A==1)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by