Allocate certain numbers to create a new matrix from A and B

조회 수: 2 (최근 30일)
JL
JL 2019년 8월 28일
댓글: madhan ravi 2019년 8월 28일
Hi everyone, just having this thought. I have two matrices A and B.
A = [1 1 1 0;
1 1 0 0;
0 1 0 1;];
B = [1 2;
1 3;
2 4;
1 5;];
I want to create C by allocating each row of B, depending whether A rows has zero or one. For example, row 1 of A with 1 1 1 0, first coloum is 1 so allocate 1 2; colum 2 is 1 3; column 3 is 2 4; column 4 is empty. Image below how it should look like in C.
Table.png

채택된 답변

madhan ravi
madhan ravi 2019년 8월 28일
c=repmat(1:size(A,2),size(A,1),1);
v=(A==1) .* c;
vv=cell2mat(arrayfun(@(x) B(x,:),nonzeros(v.'),'un',0));
Rows_of_A=mat2cell(vv,sum(A,2));
celldisp(Rows_of_A)
  댓글 수: 10
JL
JL 2019년 8월 28일
Thanks Madhan, here is an illustriation
Loop.png
madhan ravi
madhan ravi 2019년 8월 28일
You would be better off, if you ask a new question , I am not able to cope up with what you want or which one you're after , after all you already get the result which you showed in the original question.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by