How can I make this following matrix
Show older comments
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 Comments
Azzi Abdelmalek
on 6 Sep 2012
it's not clear for me
Febri
on 6 Sep 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).
I do not understand the meaning of "Matrix C is matrices that show how many zeros before value in matrice A."
Image Analyst
on 6 Sep 2012
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
on 6 Sep 2012
Image Analyst
on 6 Sep 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
on 10 Sep 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
Accepted Answer
More Answers (1)
Andrei Bobrov
on 7 Sep 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));
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!