Convert a 3-d matrix to a 2-d matrix

조회 수: 16 (최근 30일)
Vishal
Vishal 2013년 8월 23일
I have this variable 'a'. I want to convert it into a 2-d matrix of dimensions 3*3. The output I am looking for is a 3*3 matrix denoted by 'b'. Any help will be appreciated.
a=randi(10,3,3,2)
val(:,:,1) =
1 3 8
6 4 8
3 1 6
val(:,:,2) =
4 10 6
9 9 4
6 4 7
b =
14 310 86
69 49 84
36 14 67

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 8월 23일
편집: Andrei Bobrov 2013년 8월 23일
val(:,:,1) =[ 1 3 8
6 4 8
3 1 6];
val(:,:,2) =[ 4 10 6
9 9 4
6 4 7];
val(:,:,3) = randi([5,15],3,3)
l = floor(log10(val)+1);
ex = cumsum(l(:,:,end:-1:2),3);
c = cat(3,val(:,:,1:end-1).*10.^ex(:,:,end:-1:1),val(:,:,end))
out = sum(c,3);
or
z = num2cell(cellfun(@num2str,num2cell(val),'un',0),[1 2])
out = str2double(strcat(z{:}))

추가 답변 (2개)

Jan
Jan 2013년 8월 23일
b = zeros( size(a, 1), size(a, 2) );
for i = 1 : size(a, 3);
b = b + 10.^(size(a, 3) - i) * a(:,:,i);
end;

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 23일
편집: Azzi Abdelmalek 2013년 8월 23일
out=val(:,:,1).*10.^(fix(val(:,:,2)/10)+1)+val(:,:,2)
  댓글 수: 1
Vishal
Vishal 2013년 8월 23일
Thanks. Not sure how i could implement this for when there are upto 5 dimensions i.e. a=randi(10,3,3,5)

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

Community Treasure Hunt

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

Start Hunting!

Translated by