How to merge rows in an array

Hi,
I have a 399x7 double array like this:
I wonder how it's possibe to create a new array with each row merged together to form an array like:
Thanks!

 Respuesta aceptada

Star Strider
Star Strider el 31 de En. de 2020
Try this:
A = randi(99, 7, 4)
Amerged = str2num(sprintf([repmat('%d',1,size(A,2)) '\n'],A.'))
producing (in this run):
A =
53 60 11 40
10 96 93 28
41 43 19 4
11 69 27 67
12 76 79 43
78 43 49 45
29 65 77 61
Amerged =
53601140
10969328
4143194
11692767
12767943
78434945
29657761
The str2double function is preferred for several reasons, however it will not work here to produce the result you want.
If you want ‘Amerged’ as a character array instead, omit the str2num call:
Amerged = sprintf([repmat('%d',1,size(A,2)) '\n'],A.')
It is necessary to transpose the matrix (here ‘A’) to get it tyo print correctly.

4 comentarios

Stephen23
Stephen23 el 31 de En. de 2020
sscanf is preferred over both str2double and str2num, it is much more efficient:
>> A = randi(99, 7, 4)
A =
43 63 17 42
46 57 12 11
10 84 60 20
44 1 64 32
4 51 92 75
34 78 18 81
72 63 54 1
>> sscanf(sprintf([' ',repmat('%u',1,size(A,2))],A.'),'%u')
ans =
43631742
46571211
10846020
4416432
4519275
34781881
7263541
Star Strider
Star Strider el 31 de En. de 2020
@Stephen — Noted. Thank you.
Fernando
Fernando el 31 de En. de 2020
@Star Strider
@Stephen
Thank you!
Star Strider
Star Strider el 31 de En. de 2020
As always, our pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Preguntada:

el 31 de En. de 2020

Comentada:

el 31 de En. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by