Multiplying the row numbers and column numbers, NOT THE ELEMENTS iN THEM
5 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2015 年 8 月 6 日
編集済み: Stephen23
2015 年 8 月 6 日
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
採用された回答
Cedric
2015 年 8 月 6 日
編集済み: Cedric
2015 年 8 月 6 日
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 件のコメント
その他の回答 (1 件)
Stephen23
2015 年 8 月 6 日
編集済み: Stephen23
2015 年 8 月 6 日
>> 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 件のコメント
James Tursa
2015 年 8 月 7 日
編集済み: James Tursa
2015 年 8 月 7 日
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
2015 年 8 月 7 日
編集済み: Cedric
2015 年 8 月 7 日
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' ) ;
参考
カテゴリ
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!