Reshape a matrix into vector using rows

14 vues (au cours des 30 derniers jours)
Mitchel Stewart
Mitchel Stewart le 29 Fév 2020
Commenté : Mitchel Stewart le 29 Fév 2020
How do I reshape a matrix into a vector where each row is positioned end to end?
%example 2d matrix
a = [1 2;3 4]
%this version reshapes columns not rows
b = reshape (a,[1,4])
The result I am looking for is:
Thanks for any assistance.
b = 1 2 3 4

Réponse acceptée

James Tursa
James Tursa le 29 Fév 2020
Modifié(e) : James Tursa le 29 Fév 2020
b = reshape(a.',1,4);
MATLAB array memory is column-wise, so in memory the "a" elements are stored 1,3,2,4. The transpose puts the elements in memory in the order 1,2,3,4 so that the reshape works as you want.
  1 commentaire
Mitchel Stewart
Mitchel Stewart le 29 Fév 2020
Thanks for the quick response!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by