【问题标题】:MATLAB: Splitting a matrix into vectorsMATLAB:将矩阵拆分为向量
【发布时间】:2014-12-12 14:15:17
【问题描述】:

我是新来的,我的第一个问题,所以请多多包涵……

我收集运动分析数据,原始数据格式为 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;

(其中“标准化”是我编写的另一个函数,用于将向量标准化为 101 个数据点)

我有一个问题,现在我正在处理具有不同列数的矩阵,有时只有几列,有时多达 100 个。我想使用 for 循环,但正在努力解决如何存储每个列的向量。

到目前为止,我必须:

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

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

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

【问题讨论】:

  • 你想对这些向量做什么?
  • 最好更改您的 normalisation 函数以使用矩阵而不是更改您的输入!
  • 另外,ask not about your attempted solution but your actual problem.。根据您的函数 normalisation 正在做什么,所有这些都可以通过 kamtal 建议的测试数据矩阵以矢量化方式完成。
  • 啊,是的,我看到我在那里陷入了 XY 问题。感谢您通知我!
  • 实际问题是:我有许多矩阵,每个矩阵都有不同数量的 N 列。每列可能有不同数量的行。我需要对每一列进行标准化,使其有 101 个数据点。

标签: matlab matrix vector


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多