如何重塑和交织矩阵元素? [英] How to reshape and interleave matrix elements?
问题描述
具有时间序列的值,我想将其重塑为nx4矩阵[X y],目的是将这些值用作机器学习算法的输入和输出值.
Having the values of time sequence, I would like to reshape it into a nx4 matrix [X y], for the purpose of using these values as input and output values for machine learning algorithm.
X(i)是1x3输入向量,y是输出标量值.
X(i) is a 1x3 input vector and y is output scalar value.
该算法将每个第2个序列值(3个值)作为输入,以预测第4个值.
The algorithm takes as an input every 2nd sequence value (3 values) in order to predict the 4th value.
举一个实际的例子,假设我们有一个序列
To give a practical example, let's say we have a sequence
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
[X y]矩阵应为:
The [X y] matrix should be the following:
[1 3 5 7; 2 4 6 8; 9 11 13 15; 10 12 14 16]
为了获得第二行,我编写了以下代码:
To get every second row I wrote the following code:
vec1 = timeSeries(1:2:end);
XyVec1 = reshape(vec1,4,[])'
类似地,它可以写成偶数:
similarly it could be written to get even numbers:
vec2 = timeSeries(2:2:end);
XyVec2 = reshape(vec2,5,[])'
我不知道该怎么做的是交织矩阵vec1和vec2行以获得
The thing that I don't know how to do is to interleave matrix vec1 and vec2 rows to get
[vec(1,:); vec2(1,:);vec1(2,:), vec2(2,:)...]
有人知道如何交织两个(或更多)矩阵的行吗?
Does anyone know how to interleave the rows of two (or more) matrices?
推荐答案
尝试
result = zeros(size(vec1,1)+size(vec2,1),size(vec1,2));
result(1:2:end,:) = vec1;
result(2:2:end,:) = vec2;
在正确的行中重复使用matlab索引工具或插入元素
Reuse matlab indexing facilities ot insert elements in correct rows
样本八度音阶模型: http://ideone.com/RVgmYA
这篇关于如何重塑和交织矩阵元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!