Multiplying the row numbers and column numbers, NOT THE ELEMENTS iN THEM
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Stephen23
il 6 Ago 2015
Modificato: Stephen23
il 6 Ago 2015
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
Risposta accettata
Cedric
il 6 Ago 2015
Modificato: Cedric
il 6 Ago 2015
Assuming e.g.:
nRows = 5 ;
nCols = 8 ;
you can do it this way:
prods = bsxfun( @mtimes, (1:nRows)', 1:nCols ) ;
which creates
prods =
1 2 3 4 5 6 7 8
2 4 6 8 10 12 14 16
3 6 9 12 15 18 21 24
4 8 12 16 20 24 28 32
5 10 15 20 25 30 35 40
EDIT: There are multiple way of doing it though; another would be:
prods = repmat( (1:nRows)', 1, nCols ) .* repmat( 1:nCols, nRows, 1 ) ;
3 Commenti
Più risposte (1)
Stephen23
il 6 Ago 2015
Modificato: Stephen23
il 6 Ago 2015
>> 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 Commenti
James Tursa
il 7 Ago 2015
Modificato: James Tursa
il 7 Ago 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.
Cedric
il 7 Ago 2015
Modificato: Cedric
il 7 Ago 2015
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' ) ;
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!