2016-08-01 255 views
0

我試圖乘以矩陣的第一列。我曾嘗試這樣的代碼:矢量矩陣乘法

A = imread('cameraman.tif'); 
x0 = A(:,1); 
y = A*x0; 

,但我得到了以下錯誤:

Error using * MTIMES is not fully supported for integer classes. At least one input must be scalar. To compute elementwise TIMES, use TIMES (.*) instead.

,當我我的代碼更改爲:y = A.*x0;我再次得到:

Error using .* Matrix dimensions must agree.

當我使用whos命令,我得到這個: A是一個256x256矩陣和x0是一個256x1矩陣。我不知道我的代碼有什麼問題。

回答

2

imread正在返回整數值。您必須首先將它們轉換爲浮點數與double執行乘法之前:

A = imread('cameraman.tif'); 

% Explicitly convert from integer datatype to double 
A = double(A); 

% NOW perform your multiplication 
y = A * A(:,1); 

去看待whos重要的是這是在「類別」列中的數據類型:

A = imread('cameraman.tif');t 
whos('A') 

%  Name   Size    Bytes Class  Attributes 
%  
%  A   256x256    65536 uint8 
0

您需要將矩陣轉換爲雙精度或單精度。

Ad = double(A); 

或:

As = single(A); 

由於這是一個圖像矩陣,請注意,imshow預計值是在[0,255]範圍爲UINT和[0,1]的單/雙。所以要麼在顯示之前退回到整數或縮放。

1

如前所述,您需要將數據轉換爲雙精度。您可以使用功能im2double來完成此操作。這個函數也會標準化數據。

a = uint8(randi([0,100],3,3)) 
b=im2double(a) 
b*255 
b(1,:)*b