Okay, I forgot that | is logical OR, as Walter pointed out. But surely some other symbol could be found, $,#,^. I'm starting to find BSXFUN syntax tiresome, considering the prevalent need for it.
Would a BSXFUN operator family be a good idea?
1 次查看(过去 30 天)
显示 更早的评论
I don't think the pipe symbol '|' is being used for anything in MATLAB syntax. Would it be a good idea to use it to make a family of BSXFUN operators? For example, instead of
C=bsxfun(@times,A,B)
C=bsxfun(@plus,A,B)
you could do things like
C=A|*B;
C=A|+B;
4 个评论
Walter Roberson
2013-6-2
编辑:Walter Roberson
2013-6-2
APL referred to this as "Generalized Outer Product", and relates it closely to APL's "Generalized Inner Product".
APL's outer product syntax is
o.OPERATOR
where o is (in APL character set) a raised small circle, and OPERATOR is a dyadic (binary) operator.
APL's inner product syntax is
OPERATOR1.OPERATOR2
where OPERATOR2 is a dyadic (binary) operator, and OPERATOR1 is a "reduction operator" (that is, reduces an array by one dimension, such as by summing along rows.) +.x (raised multiplication) is APL's matrix multiplication: form the outer product and then sum along a dimension.
It would be nice if MATLAB too applied some systematism to the way that it used operators. For example, MATLAB could start by defining "." as prefix that could be applied to any binary operator, so that instead of .^ and .* and ./ being separate operators specially parsed, that instead they became part of a general rule -- that dot could be followed by a function name or an operator (whose formal name was substituted), or perhaps even an anonymous function, to form a new binary operator.
To be consistent with existing MATLAB usage, the formal name of the new binary operator would be formed from the name of the existing operator but with the leading character dropped (mtimes -> times, mpower -> power, mrdivide -> rdivide). My instinct is that that would not be the best practice going forward. I have not yet come up with a good solution that applies to objects as well.
Oh dang, we already have THIS.THAT syntax in place, don't we? THIS being an object and THAT being either a method or property. Sigh, so much for using . as the generalized extension signaler. Maybe # then.. hmmm, that's used for pragmas such as %#function and %#eml but perhaps there is no real syntactical overlap there... unless of course we can define "%" as being an operator :)
"#" is used as the comment introducer in numerous shells, and some other languages.
I thought I remembered encountering "$" in the first column as indicating comments in FORTRAN, but if I did then it must have been non-standard (WATFOR maybe). "c" and "C" and "*" and "!" (Fortran 90) are what it uses. Ummm, maybe my mind has wandered back to JCL again :-(
回答(3 个)
Walter Roberson
2013-6-2
编辑:Walter Roberson
2013-6-2
Pipe is "or"
Unused symbols are: back-quote (`), octothorp (#), dollar-sign ($), and question-mark (?). Oh yes, and double-quote (")
Jan
2013-6-2
Instead of introducing new operators and limit the backward compatibility, you can implement a new class easily, which uses BSXFUN implicitly for the standard operators. See FEX: int64 class as an example. Here the standard arithmetic is implemented for INT64, which was missing in older Matlab versions. The same can be done for DOUBLE or better for a new derived data type to avoid confusions.
2 个评论
James Tursa
2013-6-2
Similar to Jan's suggestion would be to have TMW simply redefine the element-wise operators (.* , ./ , etc) to do bsxfun operations automatically. This would be backwards compatible with existing code (unless code somehow depended on getting an error condition for a mismatch in dimensions).
Knut
2013-6-2
编辑:Knut
2013-6-2
I occasionally use the kron() function, and I probably should use bsxfun() more. What I seem to be banging my head up against: I saw the light with MATLAB when I grasped the beauty and compactness of regular 2-d oriented array/vectorization. The fact that it tended to make my MATLAB scripts faster did not hurt either.
Recently, I am feeling constrained: for the kind of stuff that I am doing, MATLAB vectorization can only take me so far. I believe that what I am longing for is tensor math. Is this a direction that TMW will/should head for, or is it too exotic/hard to make fast libraries/something else?
I'd like to write:
a = ones(2,4,3);
g = cat(3,1,2,3);
%1. short-hand for:
%for t = 1:3,
% R1(:,:,t)=a(:,:,t)*g(t);
%end
R1 = a.*g;
R1(:,:,1) =
1 1 1 1
1 1 1 1
R1(:,:,2) =
2 2 2 2
2 2 2 2
R1(:,:,3) =
3 3 3 3
3 3 3 3
%2. short-hand for:
%R2 = zeros(2,4);
%for t = 1:3,
% R2=R2+a(:,:,t)*g(t);
%end
R2 = a*g;
R2 =
6 6 6 6
6 6 6 6
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!