Hi,
I have a 1 x 100 array, A. How can I reshape it so that I have a 10 x 10 double so that the first ten elements are the first row, second ten element are the second row, etc.
I tried reshape(A, [10,10]) but that arranged them down columns instead of rows. Any help would be appreciated!
Sushma

4 Kommentare

Shriram Krishna
Shriram Krishna am 28 Aug. 2020
Hello,
I know I'm 4 years late but i'll give my thoguhts for anyone who might want it for the future.
if your array us A = [1 2 3 4 5 6 ] and you want it to such that [1 2 3] is row 1 and [4 5 6] is row 2 then reshape(A,2,3) gives [1 3 5] as row 1 and [2 4 6]. The simple fix for this is to do reshape(A,3,2) and then take the transpose. Hope this helps. : )
DGM
DGM am 9 Nov. 2022
Bearbeitet: DGM am 9 Nov. 2022
Moving Martin Varghese's comment here:
To retain the same row order while translating. This is more accurate solution
Image Analyst
Image Analyst am 9 Nov. 2022
@DGM you can use the crossing arrows icon to move posts. This way it retains the original authorship.
DGM
DGM am 9 Nov. 2022
I should have mentioned that it was a comment-as-flag, not a normal answer or comment. As I mentioned in the thread on the new feature, I wish it would apply to flags as well. For some reason, I couldn't get it to @user his name either. Maybe because he has no other activity?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Weitere Antworten (1)

Image Analyst
Image Analyst am 28 Sep. 2016
Bearbeitet: Image Analyst am 28 Aug. 2020

10 Stimmen

Try optionally transposing the array first before and after reshaping and see what you get. Try several different ways - it's the best way to learn.
M = 1:100 % Row vector.
M1 = reshape(M, 10, [])
M2 = reshape(M, 10, [])'
M3 = reshape(M', 10, [])
M4 = reshape(M', 10, [])'
M1 =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
M2 =
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
M3 =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
M4 =
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by