Multiplying the row numbers and column numbers, NOT THE ELEMENTS iN THEM
Show older comments
So, i have been coming up across this function a lot in the last week or so and i haven't been able to create a succinct way of doing this calculation. What i am trying to do is have a function that multiplies the row and column numbers, NOT the elements in those rows and columns, but the row number and column number. So, for matrix coordinates (1,2), the result that should be displayed at that location should be 2 based on their multiplication. Likewise, for (8,3), it should be 24 based on the multiplication of the row and column number. Is there a way that i could do that??
1 Comment
You make it clear that you want to multiply the row and column indices, but you do not tell us what form these indices are in: are they in vector, or as individual scalars, or are you wanting to derive them directly from a matrix?
>> prod([1,2])
ans = 2
>> prod([8,3])
ans = 24
Accepted Answer
More Answers (1)
>> C = 1:5;
>> R = 1:3;
>> R(:)*C
ans =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
And of course you can derive this from any matrix using size:
>> X = [...some matrix...]
>> C = 1:size(X,2);
>> R = 1:size(X,1);
6 Comments
Cedric
on 6 Aug 2015
+10 if I could ;-)
Hey Stephen, want to laugh? Run this:
n = 1e4 ; tic ; A = (1:n)' * (1:n) ; toc
n = 1e4 ; tic ; B = bsxfun( @times, (1:n)', (1:n) ) ; toc
all( A(:) == B(:) )

James Tursa
on 6 Aug 2015
Edited: James Tursa
on 6 Aug 2015
FYI, MATLAB's outer product code has been unusually slow for many, many years. A simple C-mex code using BLAS calls, or even hand-coded loops, easily beats it for speed (and pretty much matches the bsxfun-times combo method above for timing). E.g., typical timings on my machine:
clear A B C D x y
disp('outer product')
tic ; A = (1:n)' * (1:n) ; toc
disp('times')
tic ; B = bsxfun( @times, (1:n)', (1:n) ) ; toc
disp('mtimesx BLAS mode')
mtimesx('BLAS');
tic ; x=1:n; C = mtimesx(x,'t',x);toc
disp('mtimesx LOOPS mode')
mtimesx('loops');
tic ; y=1:n; D = mtimesx(y,'t',y);toc
>> outerproducttest
outer product
Elapsed time is 0.447033 seconds.
times
Elapsed time is 0.286421 seconds.
mtimesx BLAS mode
Elapsed time is 0.277431 seconds.
mtimesx LOOPS mode
Elapsed time is 0.279653 seconds.
James Tursa
on 7 Aug 2015
Edited: James Tursa
on 7 Aug 2015
On a related note, the dot and cross functions are also unusually slow. If you have a couple of vectors x and y, simply doing x'*y is faster than dot(x,y). And writing your own cross function is faster than using MATLAB's cross. E.g., see the rather large disparity for the dot function on a pair of large vectors:
>> x = rand(10000000,1);
>> y = rand(10000000,1);
>> tic;dot(x,y);toc
Elapsed time is 0.043338 seconds.
>> tic;x'*y;toc
Elapsed time is 0.009864 seconds.
>> dot(x,y)
ans =
2499521.1368632
>> x'*y
ans =
2499521.13686321
And a cross comparison:
>> x = rand(3,1);
>> y = rand(3,1);
>> tic;for k=1:1000000;cross(x,y);end;toc
Elapsed time is 4.972826 seconds.
>> tic;for k=1:1000000;mycross(x,y);end;toc
Elapsed time is 2.345183 seconds.
>> cross(x,y)
ans =
0.0580920013986021
-0.0593163323534998
0.0175240451510475
>> mycross(x,y)
ans =
0.0580920013986021
-0.0593163323534998
0.0175240451510475
Using
function z = mycross(x,y)
z = x;
z(1) = x(2)*y(3) - y(2)*x(3);
z(2) = y(1)*x(3) - x(1)*y(3);
z(3) = x(1)*y(2) - y(1)*x(2);
end
Granted, dot and cross are more generic and can handle array inputs etc. But for simple inputs the timing differences are more than I would have expected.
That's impressive! I have been rewriting quite a few MATLAB functions actually, which were way too slow for me, but I was not expecting that much difference on functions that perform this type of fundamental/base operations!
When I looked at the source code of the functions that I was rewriting, the reason for them to be slow was often series of tests like ISMATRIX, ISREAL, etc, which were useless because they had already been performed elsewhere in my code. These tests were orders of magnitude slower that the operations that I had to perform.
I already wrote about it, but what I would love to see in MATLAB is a set of directives for enabling/disabling functions internals like the tests that I mentioned.
directive( 'builtin', 'ClassCheck', 'off' ) ;
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!