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 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 6 日
it's not clear for me
Febri
Febri 2012 年 9 月 6 日
which part made you not clear mr?
Jan
Jan 2012 年 9 月 6 日
編集済み: Jan 2012 年 9 月 6 日
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."
Image Analyst
Image Analyst 2012 年 9 月 6 日
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 2012 年 9 月 6 日
well, i have updated my question. I hope everybody can understand my question. thanks
Image Analyst
Image Analyst 2012 年 9 月 6 日
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 2012 年 9 月 10 日
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

サインインしてコメントする。

 採用された回答

Honglei Chen
Honglei Chen 2012 年 9 月 6 日

0 投票

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 件のコメント

Febri
Febri 2012 年 9 月 6 日
編集済み: Febri 2012 年 9 月 6 日
Mr how can I modify your code if Matrice A and C are 3 dimension matrix.
Honglei Chen
Honglei Chen 2012 年 9 月 6 日
I need an example. Do you need to collapse 3 dimensional matrix to 2 dimensional matrix?
Febri
Febri 2012 年 9 月 6 日
編集済み: Febri 2012 年 9 月 6 日
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 2012 年 9 月 6 日
編集済み: Febri 2012 年 9 月 6 日
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.
Honglei Chen
Honglei Chen 2012 年 9 月 6 日
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 2012 年 9 月 6 日
Mr Honglei CHen: why did you change C(:,:,2) by C. in fact C(:,:,1)~=C(:,:,2)
Febri
Febri 2012 年 9 月 6 日
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 2012 年 9 月 6 日
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 2012 年 9 月 6 日
if I have many pages of C, how can I do to make result matrix?
Febri
Febri 2012 年 9 月 6 日
Mr I still have problem If amount row in Matrix C has not same value. give me suggestion to solve this problem mr.
Honglei Chen
Honglei Chen 2012 年 9 月 6 日
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 2012 年 9 月 6 日
Really, don't have another way to construct matrix like what I want?
Honglei Chen
Honglei Chen 2012 年 9 月 6 日
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 2012 年 9 月 6 日
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
Honglei Chen
Honglei Chen 2012 年 9 月 6 日
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)

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2012 年 9 月 7 日

0 投票

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));

カテゴリ

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by