array: concatenate two columns of integers to one colum

4 ビュー (過去 30 日間)
William Sampson
William Sampson 2012 年 9 月 13 日
(Matlab novice)
I thought this would be so easy...
I have a 2-d array of integers and I want to combine two columns like this:
... 123 456 ...
... 123 456 ...
etc...
I want:
... 123456 ...
... 123456 ...
etc...
as single numbers (that is, I want (123 * 1000) + (456)
The number of rows will be variable from one run to the next.
Any help would be appreciated.
  1 件のコメント
Jan
Jan 2012 年 9 月 13 日
The question is not clear. What is the wanted output for: [1,2,3,4; 5,6,7,8; 9,10,11,12] ?

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

採用された回答

Honglei Chen
Honglei Chen 2012 年 9 月 13 日
編集済み: Honglei Chen 2012 年 9 月 13 日
a = randi([1 100],[5 4]) % a 5x4 integer matrix
b = cellfun(@num2str,{a(:,2:3)},'UniformOutput',false)
c = cellfun(@(x) strrep(x,' ',''), cellstr(b{1}),'UniformOutput',false)
a(:,2) = str2double(c)
a(:,3) = []
  2 件のコメント
Jan
Jan 2012 年 9 月 13 日
編集済み: Jan 2012 年 9 月 13 日
Faster:
b = sprintf('%d%d*', a(:, 2:3).');
c = sscanf(b, '%d*'); [EDITED, thanks Honglei]
STRREP operates on cell string directly, so there is usually no need to create a time-consuming anonymous function inside CELLFUN.
Honglei Chen
Honglei Chen 2012 年 9 月 13 日
@Jan, thanks for the tip. That's a good one. I think you missed a b in your second expression.

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

その他の回答 (4 件)

Tom
Tom 2012 年 9 月 13 日
編集済み: Tom 2012 年 9 月 13 日
A=[1 2 3;3 5 7];
B=[7 8 9;4 5 6];
%
C=[A B];
w=size(C,2);
D=10.^(w-1:-1:0);
Out=sum(bsxfun(@times,D,[A B]),2)

Jan
Jan 2012 年 9 月 13 日
編集済み: Jan 2012 年 9 月 13 日
A = [1234, 567; 1, 234];
B = A(:, 1) .* nextpow10(A(:, 2)) + A(:, 2);
function Y = nextpow10(X)
Y = 10 .^ floor(log10(abs(n)) + 1); % [EDITED]
Please test this before using, I cannot run Matlab currently.
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 9 月 13 日
編集済み: Andrei Bobrov 2012 年 9 月 13 日
A=randi(67,6,4)
n = ceil(log10(A(:,2:end)));
out = sum(A(:,1:end-1).*(10.^fliplr(cumsum(fliplr(n),2))),2)+ A(:,end)

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


William Sampson
William Sampson 2012 年 9 月 13 日
Yeah - so
1) I'm not getting it and
2) I seem to not have been clear enough (sorry...)
what I have is a data array with 59 columns and an indeterminate number of rows and I want to combine 2 (for example) columns as described above. In reality it is 5 columns, 8 - 12, of 16 digits each, but I'll start with getting 2 of them done.
So I have, e.g., rawData(:,9) and rawData(:,10) to combine
  2 件のコメント
Honglei Chen
Honglei Chen 2012 年 9 月 13 日
It's the same. I updated my answer to show an example of combining 2nd and 3rd column of a matrix
Jan
Jan 2012 年 9 月 13 日
編集済み: Jan 2012 年 9 月 13 日
See my suggestion:
B = rawData(:, 9) .* nextpow10(rawData(:, 10)) + rawData(:, 10)
result = [rawData(:, 1:8), B, rawData(:, 11:end)]
I do not see, why the suggested method should not work.
Btw. What do you expect as result of two 16 digit numbers? You can store only 16 digits in variables of the type DOUBLE.

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


Ryan
Ryan 2012 年 9 月 13 日
編集済み: Ryan 2012 年 9 月 13 日
A = [1234, 567; 1, 234]; %Sample Data
% Figure out how many spaces to shift the left column
C = ceil(log10(A(:,2)));
% Find areas where the value is a multiple of 10 or is 1
idx = A(:,2) == 1 | rem(A(:,2),10) == 0;
C(idx) = C(idx) + 1; % Handle the exception
% Determine how much to shift the left column
C = 10.^C;
% Add in the right column
A = A(:,1).*C + A(:,2);

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by