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?

 Respuesta aceptada

Thorsten
Thorsten el 1 de Dic. de 2015
C = 10.^ceil(log10(B)).*A + B;

5 comentarios

pkh
pkh el 1 de Dic. de 2015
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?
Image Analyst
Image Analyst el 1 de Dic. de 2015
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.
pkh
pkh el 1 de Dic. de 2015
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)
Thorsten
Thorsten el 1 de Dic. de 2015
Editada: Thorsten el 1 de Dic. de 2015
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.
pkh
pkh el 1 de Dic. de 2015
Yes it works as intended with unique, thank you

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 1 de Dic. de 2015
Editada: Stephen23 el 1 de Dic. de 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

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

pkh
el 1 de Dic. de 2015

Comentada:

pkh
el 1 de Dic. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by