I am doing my research. I have problem to make code to present this following matrices.
A(:,:,1)=[ 1 2 3 4;
3 2 1 4]
A(:,:,2)=[ 1 3 4 2;
4 2 1 3]
C(:,:,1)=[ 0 0 1 2;
1 1 1 0]
C(:,:,2)=[ 1 0 1 0;
0 1 1 1]
Matrices C is matrices that show how many zeros before value in matrice A.
And i have this following matrices to present value in matrices A.
value=1,
B(1)=[1 1 1;
1 1 1]
value=2,
B(2)=[2 2 2;
2 2 2]
value=3,
B(3)=[3 3 3;
4 4 4]
value=4,
B(4)=[4 4 4;
3 3 3]
So that I will get this following matrices by combining A and C.
Res(:,:,1)=[1 1 1 2 2 2 0 3 3 3 0 0 4 4 4;
1 1 1 2 2 2 0 4 4 4 0 0 3 3 3;
0 3 3 3 0 2 2 2 0 1 1 1 4 4 4;
0 4 4 4 0 2 2 2 0 1 1 1 3 3 3]
Res(:,:,2)=[0 1 1 1 3 3 3 0 4 4 4 2 2 2 0;
0 1 1 1 4 4 4 0 3 3 3 2 2 2 0;
4 4 4 0 2 2 2 0 1 1 1 0 3 3 3;
3 3 3 0 2 2 2 0 1 1 1 0 4 4 4]
I got this following from other forum.
CP = bsxfun(@minus, 3 , sum(C,2));
G = mat2cell(C,ones(size(C,1),1),ones(size(C,2),1),ones(size(C,3),1));
CP = mat2cell(CP,ones(size(CP,1),1),ones(size(CP,2),1),ones(size(CP,3),1));
D = B(A);
E = cellfun(@(x,n) [zeros(size(x,1),n) x], D, G,'UniformOutput',false);
for ii=1:depth;
E(:,end,ii) = cellfun(@(x,n) [x,zeros(size(x,1),n)],E(:,end,ii),CP(:,:,ii),...
'UniformOutput',false);
NewMatrice(:,:,ii)=cell2mat(E(:,:,ii));
end
What is your Opinion?
For starters, I don’t understand how your matrix
Aboth holds the firstA = [ 1 2 3 4; 3 2 1 4]andA(1),A(2),A(3)andA(4)at the same time.I suppose that you have the following instead:
Assuming this, you can get your desired result in the following way:
which yields:
EDIT #1:
Following the update to your question, if you have another matrix
C = [0, 0, 1, 2; 1, 1, 1, 0]that represents the number of leading zeros to insert before theBmatrices, you can get your desired result in the following way:this yields:
EDIT #2:
Following the second update to your question, if your
AandCmatrices are 3-dimensional, this adds a lot of complexity to the code. I find it easiest to use aforloop and reduce the problem to 2-D, like so:If you want different sizes for
Resin each iteration, I suggest you makeResa cell array, i.e.:P.S.
Notice that for the second iteration you have
C = [1, 0, 1, 0; 0, 1, 1, 1]you’ll get a problem, because the second row won’t have the same amount of zeroes as the first. I believe you meant something like this:C = [1, 1, 1, 0; 0, 1, 1, 1].EDIT #3:
Here’s code to generate a random matrix
C, where each row sums up to a predefined integerK(which is also chosen arbitrarily):