Reshaping (M,N)-matrix to (M,1)-matrix
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello everyone,
I have a matrix, for example:
A = [1, 2, 3;
4, 5, 6;
7, 8, 9]
and now I want to create a matrix:
A = [123;
456;
789]
Does anybody know how I can do this efficiently?
(I need to do this 62.000 times for my matrix)
댓글 수: 0
채택된 답변
Andrei Bobrov
2013년 11월 8일
편집: Andrei Bobrov
2013년 11월 8일
z = floor(log10(A)+1);
z(isinf(z)) = 1;
ex = fliplr(cumsum([zeros(size(A,1),1), fliplr(z(:,2:end))],2));
out = sum(A.*10.^ex,2);
eg:
> A =[14 10 3 16
12 0 10 18
3 11 14 7
9 13 0 20];
> z = floor(log10(A)+1);
> z(isinf(z)) = 1;
> ex = fliplr(cumsum([zeros(size(A,1),1), fliplr(z(:,2:end))],2));
> out = sum(A.*10.^ex,2)
out =
1410316
1201018
311147
913020
댓글 수: 3
추가 답변 (2개)
Jos (10584)
2013년 11월 8일
Another trick using strings:
A = [1 2 3 ; 10 11 12 ; 90 0 99]
B = str2num(sprintf([repmat('%d',1,size(A,2)) ' '],A.')).'
댓글 수: 2
Jos (10584)
2013년 11월 8일
Note that you can play around with the "%d". For instance, you can use "%02d" so that a single digit number will be have a leading zero.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!