MATLAB中2个向量的2列矩阵 [英] 2-column matrix from 2 vectors in MATLAB
问题描述
这感觉应该很简单.
我正在建立一个最小距离分类器,我想将feature1和feature2放在同一矩阵中,以便我可以调用它们并得到这样的答案.
I am building a minimum distance classifier and I want to put feature1 and feature2 into the same matrix so that I can call them and get answers like this.
featureVector(1,:) =
所有feature1
值
featureVector(2,:) =
所有feature2
值
我正在遍历,希望在循环运行时将这些值放入featureVector
.
I'm looping through and hoping to put these values into the featureVector
as the loop runs.
我是MATLAB的新手,所以我不确定如何将其表达出来.希望这有足够的道理.
I'm fairy new to MATLAB so i'm not sure how to put that into words. Hopefully that makes enough sense.
谢谢大家.
推荐答案
如果您的feature1
是水平矢量,则请执行以下操作:
If your feature1
is a horizontal vector, then do:
featureVector = [ feature1 ; feature2 ]
否则(如果是垂直的话),请执行以下操作:
Else (if vertical), do:
featureVector = [ feature1 , feature2 ]
如果您不知道它的方向,则可以随时执行reshape
:
If you don't know it's orientation, you can always do a reshape
:
feature1 = reshape(feature1, 1, numel(feature1))
...是什么使feature1
水平,或者:
... what will make feature1
horizontal, or:
feature1 = reshape(feature1, numel(feature1), 1)
...什么会使feature1
垂直.
... what will make feature1
vertical.
reshape
将从任何向量/矩阵生成水平/垂直向量,并与原始来源一一对应地取特定值.
reshape
used like that will make horizontal/vertical vector from any vector/matrix, taking particular values one-by-one from the original source.
编辑:有效的证明:
>> a = [1 ; 2 ; 3 ; 4 ; 5];
>> b = [6 ; 7 ; 8 ; 9 ; 10];
>> ab = [a, b]
ab =
1 6
2 7
3 8
4 9
5 10
>> ab(2,:)
ans =
2 7
编辑:如果您的feature1
和feature2
是标量,并且您想在每次迭代中将它们分别添加到featureVector
中,请执行以下操作:
Edit: If your feature1
and feature2
are scalars, and you want to add them to the featureVector
one-by-one in every iteration, then do:
featureVector = []
for i = 1:...
feature1 = ...;
feature2 = ...;
featureVector = [featureVector; [feature1, feature2]];
end
这篇关于MATLAB中2个向量的2列矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!