Create a matrix of average Values from a Vector
1 次查看(过去 30 天)
显示 更早的评论
Hi all,
It would be great if you could help me solve this problem!
I have a vector of 5 numbers [2 4 8 9 12] and want to create a matrix with the size 5*5 containing the average of each of the respective pairs of numbers in the corresponding field of the matrix. In this case it would be
[2 3 5 5.5 7;
3 4 6 6.5 8;
5 6 8 8.5 10;
5.5 6.5 8.5 9 10.5;
7 8 10 10.5]
How could I implement this with code? Would this be the right implementation?
vector = [2 4 8 9 12];
for i = 1:5
for j = 1:5
matrix(i,j) = (vector(i) + vector(j))/2;
end
end
matrix
Thanks!
Johanna
回答(1 个)
KSSV
2022-1-2
The same implementation without loop:
matrix = (vector+vector')/2 ;
When you have used a loop, you need to initilaizethe matrix into zeros.
matrix = zeros(5) ;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!