本文介绍了如何用标量替换向量的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个向量(或数组)
I have a vector (or array)
A = [2 3 4]
我想用标量值4替换每一行的所有元素.我有代码:
I want to replace all elements of each row with a scalar value of 4. I have the code:
for ii = 1: size(A, 1) A(ii,:) = 4end
for ii = 1: size(A, 1) A(ii,:) = 4end
这将返回A = [4 4 4]
.如何获得1x1
矩阵:A = [4]
而不是1x3
矩阵?
This returns A = [4 4 4]
. How can i obtain a 1x1
matrix: A = [4]
rather than the 1x3
matrix?
非常感谢.
推荐答案
谢谢大家的评论.尽管它说我应该分配一个标量,但没有说我如何有效地做到这一点.这是有效的方法:
Thank you guys for your comments. Although it says i should assign a scalar, it didn't say how i could do that effectively. Here is what works:
A = [2 3 4; 1 3 2];
for ii = 1: size(A, 1)
B(ii,:) = A(ii,3)
end
这篇关于如何用标量替换向量的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!