How can I make this following matrix

Actualy this question related with my question before matlabcentral/answers/47516-how-can-make-code-to-present-this-following-case. Although I just add one matrix C but i cannot to solve this case. so any one help me please.
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 of element matrix A in matrix result..
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]
thanks for your help

7 comentarios

Azzi Abdelmalek
Azzi Abdelmalek el 6 de Sept. de 2012
it's not clear for me
Febri
Febri el 6 de Sept. de 2012
which part made you not clear mr?
Jan
Jan el 6 de Sept. de 2012
Editada: Jan el 6 de Sept. de 2012
While you, Febri, know, what your "question before" is, remember, that the users of this forum read about 100 messages per day. Therefore some of us will not know, what you are talking about. Please insert a link to the questions (by editing, not as comment).
Please read About tags and choose better tags.
I do not understand the meaning of "Matrix C is matrices that show how many zeros before value in matrice A."
OK, look at the line:
A(:,:,1)=[ 1 2 3 4;
Now that row of A clearly does NOT have ANY zeros in it, yet the corresponding row of C is this:
C(:,:,1)=[ 0 0 1 2;
which indicates that the 3 in A saw 1 zero before it in the row (somewhere in columns 1-2), and the 4 of A saw 2 zeros before it in the row (somewhere in columns 1-3). So how could that be? There are NO zeros at all anywhere in A! Can you see why everybody is confused?
Febri
Febri el 6 de Sept. de 2012
well, i have updated my question. I hope everybody can understand my question. thanks
Image Analyst
Image Analyst el 6 de Sept. de 2012
No, it's no clearer. I'm getting confused where all the numbers in Res come from: A or B. I suggest you make the B number different than the A numbers so we can tell. Use numbers 5 through 9 in B so we can tell where they went to in Res. As is it, I don't know if a 4 in Res came from A or B.
Walter Roberson
Walter Roberson el 10 de Sept. de 2012
Febri, please add meaningful tags to this question to make it easier to categorize. See http://www.mathworks.co.uk/matlabcentral/answers/43073-a-guide-to-tags

Iniciar sesión para comentar.

 Respuesta aceptada

Honglei Chen
Honglei Chen el 6 de Sept. de 2012
A = [1 2 3 4;3 2 1 4]
B{1} = [1 1 1;1 1 1];
B{2} = [2 2 2;2 2 2];
B{3} = [3 3 3;4 4 4];
B{4} = [4 4 4;3 3 3];
C = [0 0 1 2;1 1 1 0];
C = mat2cell(C,ones(size(C,1),1),ones(size(C,2),1));
D = B(A);
E = cell2mat(cellfun(@(x,n) [zeros(size(x,1),n) x], D, C,'UniformOutput',false))

15 comentarios

Febri
Febri el 6 de Sept. de 2012
Editada: Febri el 6 de Sept. de 2012
Mr how can I modify your code if Matrice A and C are 3 dimension matrix.
Honglei Chen
Honglei Chen el 6 de Sept. de 2012
I need an example. Do you need to collapse 3 dimensional matrix to 2 dimensional matrix?
Febri
Febri el 6 de Sept. de 2012
Editada: Febri el 6 de Sept. de 2012
No, I didnt. My matrix A and C is 3 dimensional size Matrix Mr. actually matrix A has size (3,4,2) and C(3,4,2).
Febri
Febri el 6 de Sept. de 2012
Editada: Febri el 6 de Sept. de 2012
U=2,
for z=1:U
C(:,:,z) = mat2cell(C(:,:,z),ones(size(C,1),1),ones(size(C,2),1));
D(:,:,z) = B(A(:,:,z));
E(:,:,z) = cell2mat(cellfun(@(x,n) [zeros(size(x,1),n) x],...
D(:,:,z),C(:,:,z),'UniformOutput',false));
end
I tried to edit like above edited code. but it did not work.
A = [1 2 3 4;3 2 1 4];
A(:,:,2) = A
B{1} = [1 1 1;1 1 1];
B{2} = [2 2 2;2 2 2];
B{3} = [3 3 3;4 4 4];
B{4} = [4 4 4;3 3 3];
C = [0 0 1 2;1 1 1 0];
C(:,:,2) = C;
C = mat2cell(C,ones(size(C,1),1),ones(size(C,2),1),ones(size(C,3),1));
D = B(A);
E = cell2mat(cellfun(@(x,n) [zeros(size(x,1),n) x], D, C,'UniformOutput',false))
Febri
Febri el 6 de Sept. de 2012
Mr Honglei CHen: why did you change C(:,:,2) by C. in fact C(:,:,1)~=C(:,:,2)
Febri
Febri el 6 de Sept. de 2012
Mr, this following code is My edited code inspired your code. But it will have probelm if amount per row in matrix C has not same value. how to modify this code yah?
C = mat2cell(C,ones(size(C,1),1),ones(size(C,2),1),ones(size(C,3),1));
D = B(A);
for k=1:2;
E(:,:,k) = cell2mat(cellfun(@(x,n) [zeros(size(x,1),n) x],...
D(:,:,k), C(:,:,k),'UniformOutput',false));
end
Honglei Chen
Honglei Chen el 6 de Sept. de 2012
I'm just giving an example, you can have anything you want in the second page of C.
For your example to work, each row of C has to add up to the same amount, otherwise you just cannot put your result in a matrix. If a cell is ok for you, you can simply remove cell2mat and your result will be in a cell.
Febri
Febri el 6 de Sept. de 2012
if I have many pages of C, how can I do to make result matrix?
Febri
Febri el 6 de Sept. de 2012
Mr I still have problem If amount row in Matrix C has not same value. give me suggestion to solve this problem mr.
Like I said, in this case you cannot form your Res because you don't have the same number of elements in each row. One way to get around is to keep result in cell instead.
A = [1 2 3 4;3 2 1 4]
B{1} = [1 1 1;1 1 1];
B{2} = [2 2 2;2 2 2];
B{3} = [3 3 3;4 4 4];
B{4} = [4 4 4;3 3 3];
C = [0 0 1 2;1 1 0 0];
C = mat2cell(C,ones(size(C,1),1),ones(size(C,2),1));
D = B(A);
E = cellfun(@(x,n) [zeros(size(x,1),n) x], D, C,'UniformOutput',false)
E =
[2x3 double] [2x3 double] [2x4 double] [2x5 double]
[2x4 double] [2x4 double] [2x3 double] [2x3 double]
Febri
Febri el 6 de Sept. de 2012
Really, don't have another way to construct matrix like what I want?
Honglei Chen
Honglei Chen el 6 de Sept. de 2012
If one line has 15 elements and the other line has 14 elements, how can you tile it together to make a matrix? A matrix means that there are same number of elements in each row.
Febri
Febri el 6 de Sept. de 2012
I think we can put zeros right? as extra column in row that has 14 element right? but how can to do it yah...hemm
A = [1 2 3 4;3 2 1 4];
A(:,:,2) = A
B{1} = [1 1 1;1 1 1];
B{2} = [2 2 2;2 2 2];
B{3} = [3 3 3;4 4 4];
B{4} = [4 4 4;3 3 3];
C = [0 0 1 2;1 1 0 0];
C(:,:,2) = C;
CP = bsxfun(@minus, max(sum(C,2)), sum(C,2))
C = 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, C,'UniformOutput',false)
E(:,end,:) = cellfun(@(x,n) [x,zeros(size(x,1),n)],E(:,end,:),CP,'UniformOutput',false)
cell2mat(E)

Iniciar sesión para comentar.

Más respuestas (1)

Andrei Bobrov
Andrei Bobrov el 7 de Sept. de 2012
q1 = cell(size(C).*[1 2 1]);
q1(:,1:2:end,:) = arrayfun(@(x)zeros(2,x),C,'un',0);
q1(:,2:2:end,:) = B(A);
[s,s] = cellfun(@size,q1);
s2 = sum(s,2);
q2 = num2cell([q1,arrayfun(@(x)zeros(2,x),max(max(s2)) - s2,'un',0)],2);
out = cell2mat(cellfun(@cell2mat,q2,'un',0));

Categorías

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 6 de Sept. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by