MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by David Rudolph on 7 Jun 2014

Here is my code thus far:

function [ outData ] = splitTrial( rawData, numTrial ) %SPLITTRIAL Summary of this function goes here % Detailed explanation goes here

numRows = round(length(rawData)/(numTrial)); outData = zeros(numRows, numTrial); for i = 1:numTrial

outData(:,i) = rawData(1,:);

end end

rawData represents loaded EMG data with 3605 elements ( 3605 X 1 matrix). The number of columns for outData is simply the number of trials. I can't, however, come up with a general algorithm to replace column wise the elements from the raw data into outdate with the proper matrix dimensions specified in the above reallocated matrix. Any help would be greatly appreciated!

*No products are associated with this question.*

Answer by dpb on 7 Jun 2014

Edited by dpb on 8 Jun 2014

Also format the code you do have so it's legible...if it doesn't look right in the preview window the posting isn't ready for prime time.

It would seem that if the number of trials is evenly divisible into the length of the vector (and if it's not, you need another variable vector to indicate the length of each trial) that the output rearranged would simply be

nPerTrial=length(rawdata)/numtrials); outdata=reshape(rawdata,nPerTrial,[]);

This will give *nPerTrial* columns.

As noted above, if

mod(length(rawdata),numtrials)~=0

then you've got a problem that's insoluble w/o additional information.

Answer by Star Strider on 10 Jun 2014

Is this what you want?

A = [ 1 3 5; 2 4 6]; B = [0 0 0; 0 0 0];

B(1:2,1) = A(1:2,1)

produces:

B =

1 0 0 2 0 0

Answer by dpb on 10 Jun 2014

*...How do I index an entire column of a matrix but only part of a row of that same matrix?*

Star's answer generalized...

A(r1:r2,col)

is the submatrix from row *r1* thru *r2* for the column *col*. Write an address expression of any sort for the assignment that is of commensurate size in each dimension for the target.

The above can be expanded to include a specific delta rather than the default unitary step as

A(r1:step:r2,col)

See

doc colon

and the "Getting Started" documentation for details.

I wonder again, though, regarding the original problem that raised the question if there isn't a more expeditious solution if you'd define the actual data structure more fully. Is there an issue in an erroneous assumption in my previous answer?

## 4 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/132771#comment_218508

Is each

(3605x1)matrix one trial and type, or does it consist of several trials or types? How are your data orgainsed within vectors and between vectors?It’s not at all clear what you want to do with your data. We’re left guessing, and for all our other talents, we’re not particularly good at mind-reading.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/132771#comment_219059

Let me rephrase the portion of the problem I am struggling with. How do I index an entire column of a matrix but only part of a row of that same matrix? For example, if I had matrix A = [ 1 3 5; 2 4 6] and matrix B = [0 0 0; 0 0 0], how would I index column 1 of matrix A to replace the first two elements of matrix B with those numbers?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/132771#comment_219064

I decided to put my reply as an Answer ...

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/132771#comment_220176

So David, which one of these fine answers are you officially going to "Accept". Note that you can "Accept" only one but you can vote for all of them, not just the one that you accept. If there's no answer yet, then explain what the current problem is.