Copy elements to end of row vector - help needed!
Show older comments
Dear Matlab forum,
For my research I have a full EMG row matrix that includes stance and swing phases.
To make this easy to answer, I'll make an example:
Let's call the EMG matrix, matrix 'a', in which stance and swing phases are included. In matrix 'b', in the rows, the indices are mentioned of where the stance phase in matrix a will start and where the stance phase will stop. Here, that would be from row 1:2, row 4:5, row 7:8 etc.
Simply put I desire to write a row matrix 'c', where all the stance phase part will be pasted. In this example c should look like [10 10 20 30 40 40 50 60]'.
How should I define (???) to let the loop fill [c] as a row vector mentioned above? As I'm a novice in this topic, I hope you can help me out. At this point
[c] = [10 20 40 50
10 30 40 60]
and should look like [c] = [10 10 20 30 40 40 50 60];
Thanks,
Jerome
--------------------------------------------------------------------------------------------
a = [10 10 20 20 30 30 40 40 50 50 60 60 70 70 80 80 90 90]';
b = [1 4 7 10;
2 5 8 11];
c = []';
% test
c1 = a( b(1,1) : b(2,1),1 )
c2 = a( b(1,2) : b(2,2),1 )
c3 = a( b(1,3) : b(2,3),1 )
for i = 0 : 3
?????????????????????????
c (:,end+1) = a( b(1,1+i) : b(2,1+i), 1)
???????????????????????
end
Accepted Answer
More Answers (1)
dpb
on 2 Jul 2020
A)
c=c(:).';
B)
" matrix 'a', in which stance and swing phases are included. In matrix 'b', in the rows, the indices are mentioned of where the stance phase in matrix a will start and where the stance phase will stop. Here, that would be from row 1:2, row 4:5, row 7:8 etc."
To extract the rows of 'b' from a' where 'b' is the pair of rows as above, consider that 'b' is everything except rows 3, 6, 9, ... use that fact:
b=a; % copy the original first
b(3:6:end,:)=[]; % remove unwanted rows
alternatively, can write
i1=[1:3:size(a,1)].; % vector of first rows of b (1, 4, 7, ...)
i2=i1+1; % and the next row after (2, 5, 8, ...)
b=a([i1;i2],:); % select rows from a for b
Note one can dispense with the explicit i2 variable above as well...
b=a([i1;i1+1],:); % select rows from a for b
creates i2 as temporary w/o explicit name.
Look up addressing operations in the "Getting Started" documentation
2 Comments
Jeroen Vermeulen
on 2 Jul 2020
dpb
on 2 Jul 2020
You said
[c] = [10 20 40 50
10 30 40 60]
and should look like ...
[c] = [10 10 20 30 40 40 50 60];
That's what it does... (:) creates column vector, (.') transposes to row.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!