结肠的功能形式(:)操作员?
116次观看(最近30天)
显示较旧的评论
This is a post about "how to write neat, efficient, readable MATLAB code". I get the job done by doing work-arounds, but I would rather not do those.
假设A,B是两个未知方向的向量,我们希望计算与点产品相似的东西。
看起来像这样:
函数y = myfunc(a,b)
//确保that vectors are oriented the same way
y = a(:)。* b(:);
但是,如果我需要计算y元素2:n怎么办?我们可能会做这样的事情,但我认为它会削减代码:
函数y = myfunc(a,b)
a = a(2:end)
b = b(2:结束)
y = a(:)。* b(:);
I am wishing for a functional form of the colon operator that might look something like this:
函数y = myfunc(a,b)
y =结肠(a(2:end))。*结肠(b(2:end));
0评论
接受的答案
更多答案(3)
Daniel Shub
2012年9月18日
I thought that
挤
会做您想做的事,但显然它不能在行矢量上“工作”。很好的是
挤
有一个行旗。除此之外,您可以轻松创建自己的结肠功能。从
类型挤
您需要更改的内容应该很明显。
0评论
Royi Avital
2017年10月10日
I really wish MATLAB would add function to vectorize arrays into column vector as the colon operator does.
Jan
2017年10月10日
有什么问题:
y = x(:)
或者:
重塑(x,[],1)
There is a functional form of the x(:) operator:
subsref (x,结构('类型',,,,'()',,,,“潜艇”,,,,{{“:”}})))
1条评论
沃尔特·罗伯森(Walter Roberson)
2018年10月17日
Sometimes it is easiest to define an auxillary function, such as
列= @(m)m(:);
之后你可以
列(a(2:end))。*列(b(2:end))