flat

Version 1.0.0.0 (255 octets) par Vincent
flatten a matrix, i.e. make it a vector
1,3K téléchargements
Mise à jour 31 oct. 2007

Aucune licence

Warning: this is obviously a pretty dumb function, that will be useful when you need to often vectorize indexed matrices.

In matlab you usually vectorize a multidimensional matrix by indexing it with (:), e.g. x(:).

However, if you are trying to vectorize only selected columns or lines in the matrix this is not possible, meaning you can't do something like

hist (x(index,:))
sum (x(index,:))

The usual workaround implies copying the needed elements as a new matrix, as in

x2 = x(index,:);
hist (x2(:));
sum (x2(:));

which crowds your namespace with useless variables and makes your code more complex. Using the (allegedly dumb, but convenient) flat() function, this becomes

hist(flat(x(index,:)));
sum(flat(x(index,:)));

which is both easy to understand, clean and doesn't store the intermediate values.

This was inspired by the numpy x.flat() function.

Citation pour cette source

Vincent (2026). flat (https://fr.mathworks.com/matlabcentral/fileexchange/17257-flat), MATLAB Central File Exchange. Extrait(e) le .

Compatibilité avec les versions de MATLAB
Créé avec R2006b
Compatible avec toutes les versions
Plateformes compatibles
Windows macOS Linux
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Help Center et MATLAB Answers
Version Publié le Notes de version
1.0.0.0

insert a warning in the description