Solving Matrix Multiplication Problems with Matlab 7.0

In summary: In this case, since A is a 1 by 11 vector and B is a 1 by 3 vector, the '*' operator will multiply the ith component of A by the ith component of B.
  • #1
arizonian
18
2
I have homework due and no help from the TA's due to a language problem.

How do you multiply a scalar with a matrix? It seems so simple, but I keep getting errors, such as "matrix must be square".

.m funtion code:

function X=parabola(a,x,b)
X=(x*a+b)^2;

Command window code :

EDU>> x=0:10:100;
EDU>> a=2;
EDU>> b=4;
EDU>> f=parabola(x,a,b)
? Error using ==> mpower
Matrix must be square.

Error in ==> parabola at 2
X=(x*a+b)^2;
EDU>>

so now we try

EDU>> f=parabola(x',a,b)
? Error using ==> mpower
Matrix must be square.

Error in ==> parabola at 2
X=(x*a+b)^2;
EDU>>

I know it can't be that hard, but the help section does not address this explicitly, and I have not been able to use logic to find out where I am going wrong. By the way, this is Matlab 7.0, Student version.

Thank You
Bill
 
Physics news on Phys.org
  • #2
I used many days ago...

I can say... in MATLAB everything 1 dimensional array is considered as vector and two are matrix...here may be you've to use a dot (.) before or after *
 
  • #3
The problem isn't with the scalar. You can add and multiply scalars to vectors with no problems.

arizonian said:
EDU>> x=0:10:100;

x is a 1 by 11 vector.

function X=parabola(a,x,b)
X=(x*a+b)^2;

You are trying to multiply a 1 by 11 vector by a 1 by 11 vector, which isn't possible.

What you're trying to do is have each component of the vector squared.

Anytime you need to do this, whether you're using *, /, or ^, you need to prefix the command with a '.'

You want your code to look like this:
Code:
function X=parabola(a,x,b)
X=(x*a+b).^2;

The period means do a point by point operation.
 
Last edited:
  • #4
Enigma,

Thank you. That is exactly what I was trying to do. I even went so far as to transpose the matrix, but that did not work. Using the '.' behind the parentheses instead of inside the parentheses is what did the trick.

Now another question. How do you code this problem:

code in the .m.file, by line item

1) function [opt] = goldmax(xlow,xhigh,maxit,es,fx)
.
.
9) f1=@(fx)xl;
10) f2=@(fx)xu;
11) if (f1>=f2)
.
.
18) if (k<=maxit)

and the code in the command window is:

EDU>> t=goldmax(5,7,50,1.e-4,'parabola')
? Function 'ge' is not defined for values of class 'function_handle'.

Error in ==> ge at 18
[varargout{1:nargout}] = builtin('ge', varargin{:});

Error in ==> goldmax at 11
if (f1>=f2)


'fx' is a handle for a function, 'xl' and 'xu' are the inputs to the function, but 'f1' and 'f2' are variables, and 'k' is my counter, which equals 1 at this point. I think I am following the syntax in the Matlab help guide, but I must be missing something.

Thanks again

Bill

:smile:
 
  • #5
I've never used the '@' symbol in any of my code. The only time I can see it being useful is if you want to have a function call another function which can be changed using the input arguments. Is that what you're using it for here?

The only time I've ever used handles is with graphics, file handling, and GUIs. All of them use handles as returns from functions, so I have never needed to code the function handles explicitly.

Lines 9 & 10,

Code:
f1=@(fx)xl;
f2=@(fx)xu;

don't look like typical Matlab syntax to me.

What exactly do you want this code to accomplish?
 
Last edited:
  • #6
Simply multiply the matrix with a dot...

ie

>> A = [1 2 3;4 5 6; 7 8 9]

A =

1 2 3
4 5 6
7 8 9

then multiply A by 3

>> A.*3

ans =

3 6 9
12 15 18
21 24 27

see the dot? hope that helped
 
  • #7
You don't need the dot for scalar-vector multiplication.

You only need it if you want to multiply the ith component of a vector with the ith component of another vector, because the '*' operator is defined as a vector multiplication.
 

Related to Solving Matrix Multiplication Problems with Matlab 7.0

What is Matlab 7.0 and how is it used to solve matrix multiplication problems?

Matlab 7.0 is a software program commonly used by scientists and engineers to analyze and solve mathematical problems, including matrix multiplication. It provides a user-friendly interface for inputting and manipulating matrices, as well as a variety of built-in functions for performing calculations.

How do I input matrices into Matlab 7.0 for multiplication?

To input matrices into Matlab 7.0, you can either type them directly into the command window or use the "Matrix Editor" tool. In the command window, you can use brackets to define the elements of the matrix, separated by spaces or commas. In the Matrix Editor, you can click on the cells to enter the values of the matrix.

Can Matlab 7.0 handle matrices of any size for multiplication?

Yes, Matlab 7.0 can handle matrices of any size for multiplication. However, it is important to note that the dimensions of the matrices must be compatible for multiplication (i.e. the number of columns in the first matrix must equal the number of rows in the second matrix).

What is the difference between element-wise multiplication and matrix multiplication in Matlab 7.0?

Element-wise multiplication in Matlab 7.0 is denoted by using the "." operator, and it multiplies each element in one matrix by the corresponding element in the other matrix. Matrix multiplication, on the other hand, is denoted by using the "*" operator and follows the standard rules of matrix multiplication (i.e. multiplying rows by columns).

How can I check if my matrix multiplication solution is correct in Matlab 7.0?

In Matlab 7.0, you can use the "isequal" function to check if two matrices are equal. This can be useful for verifying the accuracy of your matrix multiplication solution. You can also use the "disp" function to display the result of your multiplication and compare it to your expected solution.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
215
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top