MATLAB:将矩阵拆分为向量

时间:2014-12-12 14:15:17

标签: matlab matrix vector

我是新来的,也是我的第一个问题,所以请耐心等待......

我收集运动分析数据,原始数据格式为n列矩阵。每列可以有不同的行数。

我需要规范化这个矩阵,以便每列有101个数据点。到目前为止,所有原始数据都有21列,因此我编写了以下函数来规范化:

 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;

(其中'normalization'是我写的另一个函数,用于将向量规范化为101个数据点)

我有一个问题,现在我正在处理具有不同列数的矩阵,有时只是少数几个,偶尔会达到100个。我想使用for循环但我很难找出如何存储每个的向量。

到目前为止,我必须:

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

但当然只是将矩阵的最后一列作为变量输出。有没有办法存储输出的每个向量。

非常感谢任何帮助!正如我所说,我是MatLab的新手,如果编码/语言有点奇怪,那么道歉。

1 个答案:

答案 0 :(得分:1)

我认为在for循环中执行此操作是个好主意。我建议你不要保存每个向量,而是在for循环中将其标准化并将其保存到输出数组中。最好在执行此操作时预先分配输出数组。你可以尝试这样的事情:

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

PS:在MATLAB中使用i和j作为变量并不是一个好主意,因为它们用于虚构单元。有关详细信息,请参阅this question