필터 지우기
필터 지우기

How to extend the matrix size by splitting up a column in two columns

조회 수: 1 (최근 30일)
There is a Matrix A, which can have variable size, for example (5x4):
A =
|10 2 0.0 6|
|10 2 18.0 6|
|10 2 1800.0 6|
|10 2 1810.0 6|
|10 2 2215.0 6|
Now there's a specific column (3rd column), which I wish to be separated in two different columns, but next to each other, like this:
A =
|10 2 0 0 6|
|10 2 0 18 6|
|10 2 18 00 6|
|10 2 18 10 6|
|10 2 22 15 6|
Such a column can also appear in the first or last column. Then it is separated in the same way. The matrix size then gets (5x5) in this case.
The numbers in the specific column are always non-negative integers and can only contain numbers between 0000 - 9999. the first two digits belong to the one column, the next two digits belong to the next column.

채택된 답변

José-Luis
José-Luis 2012년 11월 20일
편집: José-Luis 2012년 11월 20일
A= [10 2 0.0 6;...
10 2 18.0 6;...
10 2 1800.0 6;...
10 2 1810.0 6;...
10 2 2215.0 6];
Str = mat2cell(reshape(sprintf('%04i',A(:,3)),4,[])',ones(size(A,1),1),[2 2]);
your_vals = cellfun(@(x) str2double(x),Str);
A(:,3) = [];
A = [A(:,1:2) your_vals A(:,3:end)];

추가 답변 (1개)

Jos (10584)
Jos (10584) 2012년 11월 20일
A = [1 1 1800 1 ; 2 2 1815 2 ; 3 3 6512 3]
C = A(:,3)
c2 = rem(C,100)
c1 = (C - c2)/100
output = [A(:,[1 2]) c1 c2 A(:,4)]

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by