如何将向量存储到矩阵中

123次观看(最近30天)
Amina Amzgh
Amina Amzgh on 19 Aug 2020
回答: 图像分析师 2020年8月20日
我有一个向量,在每次迭代时都采用不同的值,我想将向量的结果存储在矩阵示例中迭代1 a = [1 2 3 4]迭代2 a = [5 6 7 8],所以我想要一个结果矩阵:结果= [1 2 3 4;5 6 7 8]

答案(5)

KSSV
KSSV on 19 Aug 2020
M = 10;n = 4;
A = zeros(m,n) ;
为了i = 1:m
a(i,:) = rand(1,n);
结尾

戴维·希尔(David Hill)
为了k = 1:100
A(k,:)=randi(100,1,4);%您的向量
结尾

图像分析师
图像分析师 on 19 Aug 2020
尝试这个:
结果=零(数字,4);
为了k = 1 : numIterations
% Get A somehow, then
结果(k,:) = a;
结尾

Ruger28
Ruger28 on 19 Aug 2020
编辑:Ruger28 on 19 Aug 2020
a = [1 2 3 4];
B = [5 6 7 8];
c = [a; b]
In a loop:
sum_matrix = [];%预先分配,如果您知道您有多少个数组中的元素
a = [1 2 3 4];
为了II = 1:100迭代次数的%
% whatever math to get you your vector
b = a * ii;% 例子
sum_matrix = [sum_matrix; b];
结尾
2条评论

登录发表评论。


图像分析师
图像分析师 2020年8月20日
If the "A" is really as predictable as you implied in your comment to Ruger28 (and not general as I assumed in my Answer), and you want a vectorized way to do it, you can use reshape:
计算= 5;无论您想要/需要什么%。
结果= reshape([1:计数*4]',4,[])'
你得到:
结果=
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
当然,此矢量化版本是更MATLAB-Y的方法。根本不需要循环。(这种方式还使用您的首选和更好的变量名称“结果”。)

标签

社区寻宝

在Matlab Central中找到宝藏,发现社区如何为您提供帮助!

Start Hunting!