结肠的功能形式(:)操作员?

116次观看(最近30天)
努特
努特 2012年9月18日
评论: Royi Avital 2019年1月15日
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));

接受的答案

何塞 - 路易斯
何塞 - 路易斯 2012年9月18日
函数y = myfunc(a,b)
//确保that vectors are oriented the same way
y = reshape(a(2:end),[],1)。* reshape(b(2:end),[],1);
1条评论
Jan
Jan 2017年10月10日
很好,简短,高效:我已经接受了这个答案。

登录发表评论。

更多答案(3)

Daniel Shub
Daniel Shub 2012年9月18日
I thought that 会做您想做的事,但显然它不能在行矢量上“工作”。很好的是 有一个行旗。除此之外,您可以轻松创建自己的结肠功能。从
类型
您需要更改的内容应该很明显。

Royi Avital
Royi Avital 2017年10月10日
I really wish MATLAB would add function to vectorize arrays into column vector as the colon operator does.
3条评论
Royi Avital
Royi Avital 2019年1月15日
@Jan,我发现了类似的东西 vec(a) 要变得更加优雅。

登录发表评论。


Jan
Jan 2017年10月10日
有什么问题:
y = x(:)
或者:
重塑(x,[],1)
There is a functional form of the x(:) operator:
subsref (x,结构('类型',,,,'()',,,,“潜艇”,,,,{{“:”}})))
1条评论
沃尔特·罗伯森(Walter Roberson)
Sometimes it is easiest to define an auxillary function, such as
列= @(m)m(:);
之后你可以
列(a(2:end))。*列(b(2:end))

登录发表评论。

标签

社区寻宝

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

Start Hunting!