
#1
Oct1604, 03:53 PM

P: 18

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 



#2
Oct1604, 05:40 PM

P: 201

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
Oct1604, 08:04 PM

Emeritus
Sci Advisor
PF Gold
P: 1,817

The problem isn't with the scalar. You can add and multiply scalars to vectors with no problems.
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:




#4
Oct1704, 02:10 AM

P: 18

Matlab newbie
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.e4,'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 



#5
Oct2004, 10:33 AM

Emeritus
Sci Advisor
PF Gold
P: 1,817

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,
What exactly do you want this code to accomplish? 



#6
Oct2104, 05:07 AM

P: 19

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 


Register to reply 
Related Discussions  
Matlab: How to apply filters to and ECG signal using matlab?  Math & Science Software  2  
I am new here. I am Aniana. One of the latest member here in  General Discussion  9  
another newbie  General Discussion  50  
Help Newbie  Quantum Physics  6  
newbie here  General Discussion  12 