1
votes

I have a question to do the following however i am having difficulties, i am unsure as were to start any help will be appreciated.

Write a program which takes two matrices from the user and performs matrix multiplication. Do this using nested loops and scalar arithmetic only.

You MUST NOT use MATLAB's in­built matrix multiplication functionality. You must also throw an appropriate error message if the user enters two matrices which cannot be multiplied

1
Voting to reopen. It is perfectly possible to supply an answer that is short enough for the format. Although there are many different ways of formulating the answer, there are certainly not many possible answers - matrix multiplication is only implemented one way.Sam Roberts
@SamRoberts A lot of people use "too broad" when the reason is that it's a "gimme-teh-codez" question. I agree that this is confusing, and I think there should be a specific close reason for this purpose. In any case, questions describing requirements and asking someone to write the code for you or explain how to write the code are considered off-topic for Stack Overflow.Adi Inbar
@AdiInbar If you don't like "gimme-teh-codez" questions, you can downvote them (which specifically means that the question does not show any research effort); you can add a comment encouraging the questioner to supply their initial efforts; or you can edit the question to improve it. There has already been much debate about whether such questions have a reason to be immediately closed, and the consensus is that they should not be - there are more productive ways to deal with them. The close reasons should be used for exactly what they say and nothing else.Sam Roberts
@SamRoberts I disagree. Not only is that not the consensus, I'd say a large majority thinks this kind of question should be closed, based on extensive reading at meta and doing thousands of reviews. I always add a comment when I vote to close for this reason, and that's not mutually exclusive with voting to close. In any case, I didn't vote on this question, I was just explaining to you why it was closed as "too broad" when that reason doesn't seem to apply. That's a very common use of the "too broad" reason--which is prima facia evidence that what you said about the consensus is mistaken.Adi Inbar
Keep in mind that closing is not equivalent to immediately killing the question. That's why closed questions are labeled "on hold" for the first 5 days: to make it clear that closing is part of a process that encourages the OP to improve the question, and does not mean the question was rejected outright. Any edit automatically sends the post to the Reopen Votes queue. I think that's a much more constructive way of dealing with poor questions than downvoting. I only downvote questions that are so bad that I have a hard time understanding how the OP thought anyone could possibly answer.Adi Inbar

1 Answers

1
votes

Say you have to matrices A that is a nxp and another matrix B that is pxm, to perform matrix multiplication using only nested loops and scalar arithmetic you can use the following code:

[n,m] = size(A);
[p,q] = size(B);
C = zeros(n,p);

if p~=m
    error('Inner Matrix Dimensions Must Agree.')
end

for k = 1:n
    for j = 1:q
        temp=0;
        for i = 1:p
            temp = temp+(A(k,i)*B(i,j));
        end
        C(k,j) = temp;
    end
end