Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Matlab Problem

  • 13-11-2012 2: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,074 ✭✭✭✭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.

    You are the type of what the age is searching for, and what it is afraid it has found. I am so glad that you have never done anything, never carved a statue, or painted a picture, or produced anything outside of yourself! Life has been your art. You have set yourself to music. Your days are your sonnets.

    ―Oscar Wilde predicting Social Media, in The Picture of Dorian Gray



Advertisement