英文:
Matlab Vector division : I want to know how matlab divided the two vectors
问题
我在Matlab上进行一些编程,偶然发现了代码中的这一部分:
[1 1 1 1 1 1 1 1] / [1 3 4 7 9 11 13 15]
答案 = 0.0939
我想知道它是如何计算这个除法的。
谢谢你的时间,
我尝试了逐个元素相除,也尝试了将每个向量的元素相加然后除以。
英文:
I was doing some programming on Matlab and i stumbled upon this part in the code:
[1 1 1 1 1 1 1 1]/[ 1 3 4 7 9 11 13 15]
ans = 0.0939
I wanted to know how exactly did it compute this division
Thank you for your time,
I tried dividing element by element and i tried summing elementts of each vector and dividing it
答案1
得分: 2
/
运算符是 mrdivide
的别名,具有文档。
x = B/A 解决线性方程组 x*A = B 的系统,矩阵 A 和 B 必须包含相同数量的列。如果 A 缩放不当或接近奇异,MATLAB® 会显示警告消息,但仍然执行计算。
- 如果 A 是标量,那么 B/A 等同于 B./A。
- 如果 A 是一个 n×n 的方阵,B 是一个具有 n 列的矩阵,那么 x = B/A 是方程 x*A = B 的解,如果存在的话。
- 如果 A 是一个 m×n 的矩阵,其中 m ≠ n,而 B 是一个具有 n 列的矩阵,那么 x = B/A 返回系统方程 x*A = B 的最小二乘解。
强调一下,在你展示的情况下,其中 A
是一个 m×n 的矩阵(m=1,n=8),而 B
是一个具有 8 列的矩阵。
所以你的结果是"方程 x*A=B
的_最小二乘解_",其中你有 x=0.0939
。
你可以将其视为找到以下函数的最小值:
norm([1 1 1 1 1 1 1 1] - X*[1 3 4 7 9 11 13 15])
即,
x = 0:0.001:0.3;
y = arrayfun(@(X) norm([1 1 1 1 1 1 1 1] - X*[1 3 4 7 9 11 13 15]), x);
figure; plot(x, y)
(注意 MATLAB 不会在后台执行此绘图,这纯粹是为了让你了解这个操作表示什么)
英文:
The /
operator is an alias for mrdivide
, which has documentation.
>x = B/A solves the system of linear equations xA = B for x. The matrices A and B must contain the same number of columns. MATLAB® displays a warning message if A is badly scaled or nearly singular, but performs the calculation regardless.
>- If A is a scalar, then B/A is equivalent to B./A.
>- If A is a square n-by-n matrix and B is a matrix with n columns, then x = B/A is a solution to the equation xA = B, if it exists.
>- If A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with n columns, then x = B/A returns a least-squares solution of the system of equations x*A = B.
Emphasis mine, for the case which you are showing, where A
is a rectangular m-by-n matrix (m=1, n=8) and B
is a matrix with n=8 columns.
So your result is the "least-squares solution of the system x*A=B
", where you have x=0.0939
.
You can visualise this as finding the minimum of
norm([1 1 1 1 1 1 1 1] - X*[ 1 3 4 7 9 11 13 15])
i.e.
x = 0:0.001:0.3;
y = arrayfun( @(X) norm([1 1 1 1 1 1 1 1] - X*[ 1 3 4 7 9 11 13 15]), x );
figure; plot( x, y )
(Note MATLAB is not doing this plot under the hood, this is purely a visualisation to convince yourself what the operation represents)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论