Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Matlab Problem

  • 13-11-2012 02:07PM
    #1
    Closed Accounts Posts: 36


    Hi,
    I am new to matlab and i have been given a question i have no idea how to do. here it is
    "Write a matlab script that uses a “if” statement to check if the inner dimensions of two matrices A
    and B are the same. This program should be useable for matrices of any size so you should use the
    “size” command.
    Question 2
    If the inner dimensions the same then you must.
    (i) use nested “for” loops to determine a matrix C from the matrix product of A and B (Note: Here
    you need to use the “for” loops to sum the products of the elements of A and B, i.e.
    (this equation won't show on boards but it's here at this link http://www.mathworks.co.uk/help/matlab/ref/mtimes.html )
    Where cij
    is the (i,j) element of matrix C, etc..
    (ii) Check if the value of each element of your computed C matrix is the same as the value calculated
    using the standard Matlab matrix multiplication (D=A*B).
    (iii) For any elements of C that are not equal to the corresponding elements of D, you need to write a
    warning statement to the command window stating that the value is incorrect for that particular
    element of matrix C. It should read something like:
    “Warning: Column 3 row 4 of Matrix C is incorrect. It has a computed value of 24. Its value should be
    26”"
    and so far i have done
    "clear all
    a=[1 2 3; 4 5 6; 7 8 9]
    b=[2 4; 5 6; 7 8]
    c=a*b
    i= size(a)
    j= size(b)
    k=3
    y=2
    for n=1:y
    i=1:k
    for ;
    c(n,i)=0;
    for j=1:k
    c(n,i)=c(n,i)+ a(i,j)*b(j,n)
    end
    end
    end
    could someone who's very used to matlab please tell me where I'm going wrong or show me how to do the problem
    thanks


Comments

  • Registered Users, Registered Users 2 Posts: 13,219 ✭✭✭✭bnt


    As I read the question, you only get to the FOR loops if the inner dimensions are the same. So figure out how to do that first i.e.
    a=[1 2 3; 4 5 6; 7 8 9]
    b=[2 4; 5 6; 7 8]
    i=size(a)
    j=size(b)
    
    if i(2) == j(1) 
        (inner product calculations goes here)
    end
    

    Inner product in this case means that the number of columns in A matches the number of rows in B. Since the size function returns (rows, columns), you compare i(2) and j(1). You don't need the k and y variables, you can use i(1) and j(2) directly in the for statements.

    PS: the rest of the code looks almost right. I replaced
    i=1:k
    for ;
    
    with
    for i=1:y
    
    and added a couple of lines at the end to print out the two versions so I could see them:
    c
    d = a*b
    
    .
    PS: why re-define variables halfway through? You have i as the size of a at the start, then you re-use it later in the for loops. If this is a university assignment, the lecturer will not want to see that. It makes debugging unnecessarily difficult.

    In its pure form, fascism is the sum total of all irrational reactions of the average human character.

    ― Wilhelm Reich



Advertisement
Advertisement