How do I multiply two 'int32' data type matrices in MATLAB?
17 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2010 年 10 月 25 日
回答済み: Richard Zapor
2020 年 7 月 31 日
I would like to multiply a variable of size of (100,100) with another variable of size (100,100). Both of the variables are in 'int32' format.
採用された回答
MathWorks Support Team
2010 年 10 月 25 日
The ability to do a direct matrix multiplication operation on two ‘int32’ variables is not available in MATLAB.
You can use the following code to work around this issue:
function z = mtimes(x,y)
if (isscalar(x) || isscalar(y))
z = x .* y;
return;
end
m = size(x,1);
n = size(x,2);
if (n ~= size(y,1))
error('mmultmanual:size', 'matrix inner dimensions do not agree');
end
p = size(y,2);
z = zeros(m,p,class(x));
for i = 1:m
z(i,1:p) = sum(bsxfun(@times, reshape(x(i,:),n,1), y), 1);
end
Place the above code in a file called ‘mtimes.m’ and save this file in a directory with name ‘@int32’. This directory must be placed in a directory which is on the MATLAB path but do not add ‘@int32’ to the MATLAB path. This will give you the feature of multiplying two ‘int32’ matrices.
This code has been written only as an example for your use and is not supported by the MathWorks.
0 件のコメント
その他の回答 (2 件)
Richard Zapor
2020 年 7 月 31 日
function z = mtimes(x,y)
if (isscalar(x) || isscalar(y))
z = x .* y;
return;
end
m = size(x,1);
n = size(x,2);
if (n ~= size(y,1))
error('mmultmanual:size', 'matrix inner dimensions do not agree');
end
p = size(y,2);
z = zeros(m,p,class(x));
%May be faster than bsxfun
for rx=1:m
for cy=1:p
z(rx,cy)=sum(x(rx,:)'.*y(:,cy));
end
end
end
0 件のコメント
Richard Zapor
2020 年 7 月 31 日
A special case of uint32/64 matrix multiply is where x is a binary matrix and y is a uint32/64 vector.
This can be calculated very quickly and outputs a uint32/64 vector.
function z=bintimes64(x,y)
% z=x*y [m,n]*[n,1], where x is a binary matrix(real/int) and y is a vector uint64/uint32
% no error checking included
[m,n]=size(x);
z = zeros(m,1,class(y));
for j=1:n
v=x(:,j)==1;
z(v)=z(v)+y(j);
end
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!