简体   繁体   中英

MATLAB: Splitting a matrix into vectors

I'm new here and my first question, so please bear with me...

I collect motion analysis data, the raw data format is in a matrix of n columns. Each column can have a different number of rows.

I need to normalise this matrix so that each column has 101 data points. So far all of the raw data has had 21 columns, so I wrote the following function to normalise:

 function output = process(Test_Data)
% first step is to split the imported matrix into its component vectors:
C1 = Test_Data(:,1);
C2 = Test_Data(:,2);
C3 = Test_Data(:,3);
C4 = Test_Data(:,4);
C5 = Test_Data(:,5);
C6 = Test_Data(:,6);
c7 = Test_Data(:,7);
c8 = Test_Data(:,8);
c9 = Test_Data(:,9);
c10 = Test_Data(:,10);
c11 = Test_Data(:,11);
c12 = Test_Data(:,12);
c13 = Test_Data(:,13);
c14 = Test_Data(:,14);
c15 = Test_Data(:,15);
c16 = Test_Data(:,16);
c17 = Test_Data(:,17);
c18 = Test_Data(:,18);
c19 = Test_Data(:,19);
c20 = Test_Data(:,20);
c21 = Test_Data(:,21);

%Second step is to normalise each of the raw data variables

c1 = normalisation(C1);
c2 = normalisation(C2);
c3 = normalisation(C3);
c4 = normalisation(C4);
c5 = normalisation(C5);
c6 = normalisation(C6);
c7 = normalisation(c7);
c8 = normalisation(c8);
c9 = normalisation(c9);
c10 = normalisation(c10);
c11 = normalisation(c11);
c12 = normalisation(c12);
c13 = normalisation(c13);
c14 = normalisation(c14);
c15 = normalisation(c15);
c16 = normalisation(c16);
c17 = normalisation(c17);
c18 = normalisation(c18);
c19 = normalisation(c19);
c20 = normalisation(c20);
c21 = normalisation(c21);

%Then combine the normalised vectors into a matrix

Processed_Data = [c1; c2; c3; c4; c5; c6; c7; c8; c9; c10; c11; c12; c13; c14; c15; c16; c17; c18; c19; c20; c21];
Processed_Data = Processed_Data';

%Output the final result

output = Processed_Data;

(where 'normalisation' is another function I wrote to normalise a vector to 101 data points)

I have a problem where now I'm processing matrices that have different numbers of columns, sometimes just a few, occasionally up to 100. I'd like to use a for loop but am struggling to work out how to store each of the vectors.

So far I've got to:

NumberofColumns = size(data)
NumberofColumns = NumberofColumns(1,2)
    for i = 1 : NumberofColumns   
    Vector = data(:, i);       
end

But of course that just outputs the last column of the matrix as a variable. Is there a way of storing each of the vectors that are output.

Any help is much appreciated! As I said, I'm new to MatLab so apologies if the coding/language is a little strange.

I think doing this in a for loop is a good idea. I suggest you don't save each vector, but rather normalise it inside the for loop and save it to the output array. It is best to preallocate the output array when you do that. You could try something like this:

nCols = size(data,2);
output = zeros(101,nCols);
for k=1:nCols
    vector = data(:,k);
    output(:,k) = normalisation(vector);
end

PS: using i and j as variables is not a good idea in MATLAB, as they are used for the imaginary unit. See this question for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM