MATLAB: If i have matrix and i want to do this

functionmatrixmatrix manipulation

if i have this matrix
Matrix_row = [2 4 2 0 0
3 6 0 0 0
4 5 0 0 0
1 2 2 0 0
1 2 1 1 0
1 2 2 0 0
2 3 2 0 0
2 2 2 0 0
1 1 1 4 0
10 0 0 0 0 ]
and i compute this values
a = [ 8
9
9
5
5
5
7
6
7
10 ]
and b = [ 3
2
2
3
4
3
3
3
4
1 ]
i need to chick like this
f = zeros(10,10)
for k=1:10
if a(k) + b(k)-1 == 10 if this condition true then
go bake to Matrix_row(k) and convert this row to be a binary
(the number in Matrix_row shows how many number of ones in this row like
[2 4 2] == [1 1 0 1 1 1 1 0 1 1]
  • when the condition is true , the number in Matrix_row in that row must between the group of ones just one zero
the final solution will be
F = [ 1 1 0 1 1 1 1 0 1 1
1 1 1 0 1 1 1 1 1 1
1 1 1 1 0 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 1 0 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1 ]

Best Answer

clear all
clc
Matrix_row = [2 4 2 0 0
3 6 0 0 0
4 5 0 0 0
1 2 2 0 0
1 2 1 1 0
1 2 2 0 0
2 3 2 0 0
2 2 2 0 0
1 1 1 4 0
10 0 0 0 0 ]
a = [ 8
9
9
5
5
5
7
6
7
10 ]
b = [ 3
2
2
3
4
3
3
3
4
1 ]
f = zeros(10,10)
for k=1:10
tmp = Matrix_row(k,:)
tmp(tmp==0) = [];
if a(k) + b(k)-1 == 10
n1 = 1
for s=1:length(tmp)
f(k,n1:n1+tmp(s))=1;
f(k,n1+tmp(s))=0;
n1 = n1+tmp(s)+1;
end
end
end
f(:,11)=[];
disp(f)