Filter löschen
Filter löschen

Group values in a vector

1 Ansicht (letzte 30 Tage)
Panos Kerezoudis
Panos Kerezoudis am 26 Jun. 2023
Hi,
I have a long column vector ('trialnr', attached), whose elements are integer numbers from 1 to 55 (they index events in time-series).
I want to further concetenate the elements into groups (1-2, 3-4, 5-6, etc) so that I create blocks of rest-movement. The new vector will have the same size as the original.
Therefore, I want 1-2 to be 1, 3-4 to be 2 and so forth.
Any help would be great, thank you!
for example: if length(trialnr(trialnr==1)) is 7765 and length(trialnr(trialnr==2)) is 3548, then in the column vector these will be designated as 1 with length 7765+13548=11313.
  1 Kommentar
Star Strider
Star Strider am 26 Jun. 2023
I don’t see how this is going to work, since the ranges of the various elements (numbers of the individual elements) in the vector, although consecutive, are different —
LD = load('trialnr');
trialnr = LD.trialnr;
[Elements,Startidx,ic] = unique(trialnr, 'stable');
Lengths = diff([Startidx; size(trialnr,1)]);
LenvTable = table(Elements,Startidx,Lengths)
LenvTable = 55×3 table
Elements Startidx Lengths ________ ________ _______ 1 1 7765 2 7766 3548 3 11314 4224 4 15538 4710 5 20248 3772 6 24020 1808 7 25828 1881 8 27709 1114 9 28823 2756 10 31579 1066 11 32645 2805 12 35450 1065 13 36515 3240 14 39755 1162 15 40917 1874 16 42791 1517
.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

DGM
DGM am 26 Jun. 2023
I'm not sure if this is what you're asking for, but:
V = [1 1 1 2 2 2 3 3 3 4 4 4 5 5 5]
V = 1×15
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
V = ceil(V/2)
V = 1×15
1 1 1 1 1 1 2 2 2 2 2 2 3 3 3
  1 Kommentar
Panos Kerezoudis
Panos Kerezoudis am 1 Jul. 2023
that works great, thank you so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by