필터 지우기
필터 지우기

How to merge rows in an array

조회 수: 11 (최근 30일)
Fernando
Fernando 2020년 1월 31일
댓글: Star Strider 2020년 1월 31일
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!

채택된 답변

Star Strider
Star Strider 2020년 1월 31일
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
Fernando
Fernando 2020년 1월 31일
@Star Strider
@Stephen
Thank you!
Star Strider
Star Strider 2020년 1월 31일
As always, our pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by