Function takes as input two nonempty matrices A and B and returns the product AB
    7 ビュー (過去 30 日間)
  
       古いコメントを表示
    
    Sophie Culhane
 2020 年 10 月 20 日
  
    
    
    
    
    コメント済み: Asad (Mehrzad) Khoddam
      
 2020 年 10 月 20 日
            My task is to write a MATLAB function that takes as input two nonempty matrices A and B and returns the product AB where the number of columns in A is the same as the number of rows in B. I have a small program written so far, however it is not fully developed. I am asking for assistance on where to go next with developing a program that is accurate. I apologize that there is not much in my program so far. Here is what I have so far:
function AB = ex2(A,B)
%
%
[NRows,NCols] = size(A);
[NRows,NCols] = size(B);
AB = zeros('NRows of A, NCols') % I am not sure what to put here
for row = 1:NRows 
    for col = 1:NCols
        AB = A(row,col) * B(row,col);
    end
end
0 件のコメント
採用された回答
  Asad (Mehrzad) Khoddam
      
 2020 年 10 月 20 日
        function AB = ex2(A,B)
%
%
% we need rows and columns of both A and B
% ColsA should be equal to RowsB
[RowsA,ColsA] = size(A);
[RowsB,ColsB] = size(B);
AB = zeros(RowsA, ColsB);
for row = 1:RowsA
    for col = 1:ColsB
        AB(row,col) = sum(A(row,:)'.*B(:,col));
    end
end
2 件のコメント
  Asad (Mehrzad) Khoddam
      
 2020 年 10 月 20 日
				Yes
function AB = ex2(A,B)
%
%
% we need rows and columns of both A and B
% ColsA should be equal to RowsB
[RowsA,ColsA] = size(A);
[RowsB,ColsB] = size(B);
AB = zeros(RowsA, ColsB);
for row = 1:RowsA
    for col = 1:ColsB
        s = 0;
        for k=1:colsA:
            s = s + A(row,k)*B(k,col);
        end
        AB(row,col) = s;
    end
end
その他の回答 (1 件)
  James Tursa
      
      
 2020 年 10 月 20 日
        This is a matrix multiply, so you need to keep the rows and columns of the inputs separately.  E.g.,
[NRowsA,NColsA] = size(A);
[NRowsB,NColsB] = size(B);
Then, the product will have dimensions NRowsA x NColsB. I.e.,
AB = zeros(NRowsA,NColsB);
For your result AB, you will be looping over the size of AB, so
for row = 1:NRowsA 
    for col = 1:NColsB
        % code goes here to figure out AB(row,col)
    end
end
You would need to write code to calculate the AB(row,col) element.  You could do a dot product calculation here, or you could write another loop to interatively calculate it.
0 件のコメント
参考
カテゴリ
				Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


