how can i write this statement labels=[1;​1;1;1;1;1;​1;1;1;1;1;​1;1;1;1;1;​1;1;1;1;1;​1;1;1;2;2;​2;2;2;2;2;​2;2;2;2;2;​2;2;2;2;2;​2;2;2;2;2;​2;2;3;3;3;​3;3;3;3;3;​3;3;3;3;3;​3;3;3;3;3;​3;3;3;3;3;​3;]; in compress form?

1 Ansicht (letzte 30 Tage)
how to write it in compact form avoiding repetition

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 25 Jun. 2014
labels = kron((1:3)',ones(24,1));
or
labels = reshape(ones(24,1)*(1:3),[],1);
  3 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Roger Stafford
Roger Stafford am 25 Jun. 2014
n = 3;
r = 24;
labels = reshape(repmat((1:n),r,1),[],1);
  4 Kommentare

Melden Sie sich an, um zu kommentieren.


Matt J
Matt J am 25 Jun. 2014
Bearbeitet: Matt J am 25 Jun. 2014
Using KronProd ( link )
>> labelsKP=KronProd({ones(2400,1), (1:3).'});
you can get even more compression. For comparison,
>> labels = kron((1:3)',ones(2400,1));
>> whos labels labelsKP
Name Size Bytes Class Attributes
labels 7200x1 57600 double
labelsKP 7200x1 19660 KronProd

Community Treasure Hunt

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

Start Hunting!

Translated by