Effacer les filtres
Effacer les filtres

How to construct this matrix without loop?

1 vue (au cours des 30 derniers jours)
Ming
Ming le 28 Oct 2013
Modifié(e) : Matt J le 28 Oct 2013
Hi, everyone:
suppose I have a vector x=[a b c d e f g h ... z]. Is it possible to construct a matrix such that:
X=[a b c d; b c d e; c d e f; d e f g; e f g h; ... w x y z];
without for-loop?
many thanks

Réponse acceptée

Matt J
Matt J le 28 Oct 2013
Modifié(e) : Matt J le 28 Oct 2013
X = x(bsxfun(@plus,(0:22).',(1:4)))

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 28 Oct 2013
Modifié(e) : Azzi Abdelmalek le 28 Oct 2013
Example
x=[2 4 8 12 13 19 20 25]
out=cell2mat(arrayfun(@(ii) x(ii:ii+3),(1:numel(x)-3)','un',0))

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by