How to reshape an array horizontally

I wanted to know how to go about reshaping a character array horizontally, starting from the first index in the first row, and then the second and so on. So I want to change:
text =
3×5 char array
'words'
'words'
'words'
Into
text =
1x15 char array
'wordswordswords'
How do I do this?

 採用された回答

Star Strider
Star Strider 2020 年 9 月 22 日

2 投票

Using the reshape function:
text1 = ['words'; 'words'; 'words']
text2 = reshape(text1', 1, [])
producing:
text1 =
3×5 char array
'words'
'words'
'words'
text2 =
'wordswordswords'
.

6 件のコメント

Brett Baxter
Brett Baxter 2020 年 9 月 22 日
Thank you so much!
James Tursa
James Tursa 2020 年 9 月 22 日
@Brett: The key is to transpose the array first so that the letters line up in memory the way you want them to before doing the reshape (which preserves ordering in memory). Since MATLAB array memory is column ordered, that means lining the letters up in columns first.
Brett Baxter
Brett Baxter 2020 年 9 月 22 日
so then if I want to do operations down rows instead of columns, I have to transpose first
Star Strider
Star Strider 2020 年 9 月 22 日
Brett Baxter — My pleasure!
so then if I want to do operations down rows instead of columns, I have to transpose first
That depends on what you want to do. Here, yes.
Brett Baxter
Brett Baxter 2020 年 9 月 22 日
Understood, thank you
Star Strider
Star Strider 2020 年 9 月 22 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

その他の回答 (1 件)

Jérôme
Jérôme 2024 年 4 月 22 日

0 投票

I know it's not the class used in que question, but it's just to share how to do it with strings.
In case a string array is used instead of a char array, this can be done with the function strjoin:
text_1 = ["words" ; "words" ; "words"]
text_1 = 3x1 string array
"words" "words" "words"
text_2 = strjoin(text_1, "")
text_2 = "wordswordswords"

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

製品

リリース

R2020a

タグ

質問済み:

2020 年 9 月 22 日

回答済み:

2024 年 4 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by