Merge two columns into one column?

I have two columns that i want to merge into one column. Meaning i want
A=[123; 321] and
B=[456; 654] to be merged/combined into a new column
C=[123456; 321654]
I this possible?

 Accepted Answer

C = 10.^ceil(log10(B)).*A + B;

5 Comments

One of my elements in A equals 20001130, and the corresponding element in B equals 32378. So i want the same element in C to equal 2000113032378 But with your solution it crates the result in C in the following format: 2.000113032378000e+12
I tried format long, but it doesn't seem to solve the problem?
You didn't say that at first, so you got a solution for what you initially presented. Please give us all criteria. Like, can A be any number of digits? Is it always either 3 digit or 8 digit integers, or can it be any number of digits. Can it be double values with fractional parts to the right of the decimal point. If so, how many to the right of the decimal point do you want to include? It's always good to state these things up front otherwise you'll get what you ask for (though Thorsten did generalize it some) and spend more time arriving at a solution that you needed to.
And what's the use case for this? It seems like such an odd thing to do that it's probably just a homework assignment.
Sorry, I thought the simplified example with A and B would be faster to understand and give a general solution to.
It is not a homework assignment. I have two columns one with date(8 digits) and one with seconds(5 digits). I then use a unique function to merge rows with the same value in second, but that function doesn't account for the date. So i want to create a new column with date and second combined(13 digits)
You can use
format bank
to change the way the numbers are shown in the command window. Note that this doesn't affect your algorithms. unique will give the same results, regardless of how the numbers are actually displayed.
Yes it works as intended with unique, thank you

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 1 Dec 2015
Edited: Stephen23 on 1 Dec 2015
>> A = [123,321,20001130];
>> B = [456,654,32378];
>> F = @(n)arrayfun(@int2str,n,'UniformOutput',false);
>> cellfun(@(a,b)str2double([a,b]),F(A),F(B))
ans =
123456 321654 2000113032378

Categories

Asked:

pkh
on 1 Dec 2015

Commented:

pkh
on 1 Dec 2015

Community Treasure Hunt

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

Start Hunting!