フィルターのクリア

reshape a char array

25 ビュー (過去 30 日間)
Laura
Laura 2020 年 2 月 26 日
コメント済み: Guillaume 2020 年 2 月 26 日
Hi,
I have the following MWE and I don't understand what matlab is producing:
I start with a simple 21-length char array:
ex = 'caggtgcagctggtgcagtct'
that I would like to reshape into a 7-by-3 array. Of course, I could use a loop for this, but I thought that I could just reshape it. However, when I do
reshape(ex, [7,3])
ans =
7×3 char array
'cag'
'agc'
'gca'
'gtg'
'tgt'
'ggc'
'ctt'
This makes no sense to me. I was expecting
'cag'
'gtg'
'cag'
'ctg'
'gtg'
'cag'
'tct'
I also tried reshape(ex, 7,3) and reshape(ex(:), 7, 3) without any luck. What is matlab doing? TIA
  1 件のコメント
Guillaume
Guillaume 2020 年 2 月 26 日
Matlab is column major. This means that when you reshape something into something else, the something else is filled column by column, not row by row. So, indeed as David shows, to get the output you want you reshape then transpose.

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

採用された回答

David Hill
David Hill 2020 年 2 月 26 日
reshape(ex,[3,7])';
First number is row and second is column. Matrix is numbered down first.
  2 件のコメント
Laura
Laura 2020 年 2 月 26 日
編集済み: Laura 2020 年 2 月 26 日
wow, that is really counter-intuitive. I guess I am used to other programming languages (i.e. python) that do not "number down first"...
Thank you!
Guillaume
Guillaume 2020 年 2 月 26 日
You'll find a number of languages (C-based mostly) are row majors and an equivalent number are column-major (Matlab, Fortran, and other math-focused languages) while a few can't make their mind up (eg. OpenGL).
So, it's only counterintuitive if you've never been exposed to the other side.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by