Copy elements to end of row vector - help needed!
2 views (last 30 days)
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
0 Comments
Accepted Answer
Rik
on 2 Jul 2020
I don't fully understand your code, but looking to your results, this code should do.
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=a(b);
c=reshape(c,1,[])
3 Comments
Rik
on 2 Jul 2020
Edited: Rik
on 2 Jul 2020
Your code is quite fragile. You're trying to expand you c vector with the end keyword, but you are only creating a single index. The code below generates a large enough c vector first, then loops through b, determining the indices into a and c separately.
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];
clc
c=NaN(1,sum(1+diff(b)));
counter=0;
for n=1:size(b,2)
a_idx=b(1,n):b(2,n);
c_idx=counter+(1:numel(a_idx));
c(c_idx)=a(a_idx);
counter=c_idx(end);
end
As a further note: reshape is not very expensive in general, because normally it will only have to change the header of the variable.
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
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.
See Also
Categories
Find more on Logical 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!