How do I turn a maxtrix to a vector

So I have the following 167x6 matrix:
A B C D E F
G H I J K L
M N O P …
and so on to make a 167x6. I want it to look like (1x1002):
A B C D E F G H I J K L M N O P …
however, when I use reshape, it gives me this:
A G M B H N C I O D J P E K …
It sorts it by columns instead of rows. Please help

2 Kommentare

Jan
Jan am 5 Jun. 2018
I've formatted the code for you today. Please use the "{} Code" button in the future.
Greg Smith
Greg Smith am 5 Jun. 2018
Thank you. Sorry, I'm brand new to this.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

John D'Errico
John D'Errico am 5 Jun. 2018
Bearbeitet: John D'Errico am 5 Jun. 2018

1 Stimme

So, you cannot use reshape in a different way? Suppose you first transpose the matrix, and THEN used reshape? (Hint.)
The point is, MATLAB stores data in a matrix going down the columns in memory. So, IF you transpose the matrix first, reshape will give you what you are looking to do.

6 Kommentare

Greg Smith
Greg Smith am 5 Jun. 2018
What I don't understand is that it's saying that the number values don't match, yet I haven't added or removed any of the data points. A 167x6 matrix has the same value points as a 1x1002 vector. Why am I receiving this error?
Stephen23
Stephen23 am 5 Jun. 2018
Bearbeitet: Stephen23 am 5 Jun. 2018
"Transpose made my data out of order though,..."
Can you please show us what you tried, and the output you got.
"Is there a way to simply take row2 and put it at the end of row1?"
Yes. Transpose and reshape, just like John D'Errico wrote.
>>> B = transpose(CubeSat);
>>> C = reshape(B, [6:167,1,1002]);
error using reshape
to RESHAPE the number of elements must not change.
I apologize, I'm very new at this
reshape(CubeSat.',1,[])
Jan
Jan am 5 Jun. 2018
@Greg: No need for apologies. This is a forum for Matlab questions. You do have Matlab questions. Then the members of the forum post answers.
C = reshape(B, 1, [])

Melden Sie sich an, um zu kommentieren.

Jan
Jan am 5 Jun. 2018
Bearbeitet: Jan am 5 Jun. 2018

1 Stimme

All resorting of arrays, which keeps the neighborhood in a specific dimension, can be performed by
Y = reshape(permute(reshape(X, dim1), order2), dim3)
All you have to find is the correct definitions of the parameters. For 2D arrays, permute(T, [2,1]) can be replaced by a transpose. In some cases one or both reshapes can be omitted.
In your case:
S = ['A' 'B' 'C' 'E' 'F'; ...
'G' 'H' 'I' 'K' 'L'; ...
'M' 'N' 'O' 'P' 'Q'];
T = reshape(S.', 1, [])
PS. Inserting the quotes to produce running codes took the most time while typing this answer. Please post the data such, that they can be used by copy&paste. This is cuter than leaving the tedious work for the ones who answer.

3 Kommentare

Greg Smith
Greg Smith am 5 Jun. 2018
Thank you for the help. I will remember this for the future. What I still don't understand is the reshape is saying that I need to have the same values in order to reshape the matrix. From my understanding though a 167x6 is the same as 1x1002. Do you know why I am receiving this error?
C = reshape(B, [6:167,1,1002]);
This tries to reshape the elements of B to an array with the dimensions
[6,7,8,9,10,...,166,167,1,1002]
This would be huge.
You mean:
C = reshape(B, 1, 1002)
or
C = reshape(B, [1, 1002])
Another solution would be:
C = B(:).'
or the automatic determination of the needed value:
C = reshape(B, 1, [])
% ^ replaced automatically by 1002
Stephen23
Stephen23 am 5 Jun. 2018
Bearbeitet: Stephen23 am 5 Jun. 2018
@Greg Smith: because you specified the dimensions incorrectly. You specified the output size like this:
>> [6:167,1,1002]
ans =
6 7 8 9 10 11 ... lots of numbers here ... 166 167 1 1002
That is a really big array, with size 6x7x8x9x10x11x...x166x167x1x1002. In fact that array would have many more elements in it than there are particles in the observable universe:
>> prod([6:167,1,1002])
ans = 1.2555e+301
You probably meant to specify the size like this:
[1,1002]

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 5 Jun. 2018

Kommentiert:

am 5 Jun. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by