필터 지우기
필터 지우기

how to do this operation to calculate a new matrix ?

조회 수: 3 (최근 30일)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 30일
댓글: Firas Al-Kharabsheh 2016년 4월 30일
IF i have this matrix
A=[ 3 2 0
2 1 1
5 4 0
3 2 0
4 3 1
10 0 0 ]
and i found this matrix
a = [ 5
4
9
5
8
10 ]
and this matrix
b = [2
3
2
2
3
1 ]
i want to find this matrix F_Complete where for k=1:6 if (a(k) + b(k) - 1) == 10 then go back to A(k) and make group of ones depend on the number in the row(k) ( between each group there is zero ) like this
the third row in A = [5 4 0 ] make the condition true then
F_Complete(3,:) = [1 1 1 1 1 0 1 1 1 1]
the five row in A = [ 4 3 1 ] make the condition true then
F_Complete(5,:) = [1 1 1 1 0 1 1 1 0 1]
the last row in A = [ 10 0 0] make the condition true then
F_Complete(6,:) = [1 1 1 1 1 1 1 1 1 1]
then the final answer is
F_Complete = [ 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0
1 1 1 1 0 1 1 1 0 1
1 1 1 1 1 1 1 1 1 1 ]

채택된 답변

CS Researcher
CS Researcher 2016년 4월 30일
편집: CS Researcher 2016년 4월 30일
Try this:
N = 10;
m = size(A,1);
% Pre-allocate the F_Complete matrix
F_Complete = zeros(m, N)
for i = 1:m
if a(i)+b(i)-1 == 10
tempVector = [ones(1,A(i,1)) 0 ones(1,A(i,2)) 0 ones(1,A(i,3))];
F_Complete(i,:) = tempVector(1:N);
end
end
Hope this helps!
  댓글 수: 2
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 30일
how to apply this for column where
B=[ 0 0 1 0 1 0 1 4 1 0
10 10 6 5 1 1 1 5 6 10 ]
a1=[ 10 10 7 5 2 1 2 9 7 10 ]
b1 = [1 1 2 1 2 1 2 2 2 1 ]
how to find F_complete_column where the final will be
F_complete_column = [ 1 1 0 0 0 0 0 1 0 1
1 1 0 0 0 0 0 1 0 1
1 1 0 0 0 0 0 1 0 1
1 1 0 0 0 0 0 1 0 1
1 1 0 0 0 0 0 0 0 1
1 1 0 0 0 0 0 1 0 1
1 1 0 0 0 0 0 1 0 1
1 1 0 0 0 0 0 1 0 1
1 1 0 0 0 0 0 1 0 1
1 1 0 0 0 0 0 1 0 1 ]
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 30일
@CS Researcher

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2016년 4월 30일
편집: Matt J 2016년 4월 30일
N=10;
idx=(a+b==N+1);
F_Complete = zeros(m,N);
fun=@(x) x(1:N);
Fc=arrayfun( @(n)[ones(1,n) 0], A(idx,:),'uni',0);
Fc=arrayfun(@(i) fun( [Fc{i,:}]),(1:size(Fc,1))','uni',0);
F_Complete(idx,:)=cell2mat(Fc)
  댓글 수: 2
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 30일
give error "Undefined operator '+' for input arguments of type 'cell'"
Matt J
Matt J 2016년 4월 30일
Not for me...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by