Solving Matrix Multiplication Problems with Matlab 7.0

Click For Summary

Discussion Overview

The discussion revolves around solving matrix multiplication problems in Matlab, specifically addressing issues encountered while multiplying scalars with matrices and using function handles. Participants share their experiences and seek clarification on coding practices in Matlab 7.0.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an error encountered when trying to multiply a scalar with a matrix, indicating confusion over the correct syntax and operations in Matlab.
  • Another participant suggests that in Matlab, one-dimensional arrays are treated as vectors, and two-dimensional arrays are matrices, recommending the use of the dot operator for element-wise operations.
  • A different participant clarifies that the issue is not with scalar multiplication but rather with attempting to multiply vectors of incompatible dimensions, suggesting the use of the dot operator for pointwise operations.
  • Further, a participant expresses gratitude for the clarification and seeks help with a new coding problem involving function handles, indicating a syntax issue with the use of the '@' symbol.
  • Another participant questions the use of function handles in the provided code, expressing unfamiliarity with their application outside of specific contexts like graphics and GUIs.
  • One participant asserts that the dot operator is not necessary for scalar-vector multiplication, emphasizing its necessity only for component-wise operations between vectors.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and application of the dot operator in various multiplication scenarios, indicating that there is no consensus on some aspects of Matlab syntax and function handles.

Contextual Notes

There are unresolved questions regarding the correct use of function handles and the specific goals of the code being discussed, as well as potential misunderstandings about Matlab syntax.

arizonian
Messages
18
Reaction score
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
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 *
 
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)^[/color]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:
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:
 
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:
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K