Whats wrong with this Matlab function [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
function y = CramersRule(A,b)
[m,n] = size(A);
[o,p] = size(b);
if m~=2 | n~=2 | o~=2 | p~=1
error('the matrices must be 2*2 and 2*1')
A=[a b;c d]
b=[e;f]
X = det([e b;f a])/det([a b;c d])
Y = det([a e;c f])/det([a b;c d])
end
end
I just get the result: CramersRule([1 2;3 4], [0;4]), which was an example I used to test it.

There are several strange things here:
First of all you have an if statement that contains an error, but even though there is an error you still do things inside the same statement, perhaps you wanted an else somewhere?
Secondly you use A=[a b;c d] while a, c and d are not even defined.
Thirdly you assign to X and Y which are never even used.
Lastly you ask y as an output argument, whilst there is never an assignment to this. Perhaps you don't realize that matlab is case sensative?
All in all it is just a strange function now. Don't forget to check the mlint (warnings on the right hand side of your screen) as it can pick up most of these things.

Related

How to define a function with a matrix [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to define a two variables function g so that g(x,y) be a 2*2 matrix. To do this, I define g(x,y)=[1,1;x,y] but when I put g(1,1) I don't get any answer. How can I evaluate to g?
The code g(x,y)=[1,1;x,y] itself will not do anything. I assume that your expect result will be g=[1,1,1,1]? Therefore you should do as follow:
g=g_func(1,1);
disp(g)
function g=g_func(x,y)
g=[1,1;x,y];
end
It's not that different from the previous answer, but perhaps an anonymous function would meet your needs:
>> g = #(x,y)[1,1;x,y];
>> g(5,6)
ans =
1 1
5 6
Alternatively, if you want g to accept only one input (i.e. a 2-element vector, instead of two scalars), you could do:
g = #(x)[1,1;x(1),x(2)];
% or
g = #(x)[1,1;x(:).'];

What is wrong with [a b] = randi() [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm having trouble with this command
[a b]=randi(5,1,2)
matlab tells me
Too many output arguments.
Can anyone tell me what did I do wrong?
The reason is obvious here as there should be only one output for randi().
r = randi(imax,n)
r = randi(imax,m,n)
r = randi(imax,[m,n])
r = randi(imax,p1,...,pn)
r = randi(imax,[p1,...,pn])
r = randi(imax)
r = randi(imax,size(A))
r = randi([imin,imax],...)
r = randi(..., classname)
Check out its documentation for more info.
As #hero mentioned, you should just get a vector as output of randi.
If you really must get it to different letters, here is how it could be done:
c = num2cell(randi(5,1,2));
[a, b] = deal(c{:});
Needless to say, this is not something you should usually want.

Matlab: how to redefine indexing to begin from zero? `Subscript indices must either be real positive integers or logicals.` [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a mathematical data where it would be very convenient to have the index to start from zero like
a=sparse([],[],[],30,1);
>> a(0)=someValueHere
Subscript indices must either be real positive integers or logicals.
but Matlab by default offers only the index to start from 1. Is there some easy hack or trick by which I could still assign a(0) so that I don't need to create a dummyVar a0 for the value or append the value at the end?
So how to get assignment such as a(0) in Matlab? Every time zero-index called catch the error and return someValueHere instead of the warning?
To get MATLAB's index to start from 0 you'll need to make an large set of object classes that emulate regular numeric classes, but behave differently with functions such as subsassgn(), subsref() etc.
Maybe someone was crazy enough to do it somewhere, I'd expect this to take weeks to months of work to actually work properly.
There is a discussion on the matlab index issue: http://www.mathworks.cn/matlabcentral/newsreader/view_thread/285566
Maybe you can write a function like
function t=C_index(x)
t = x + 1;
Then you can write something like y(C_index(0)) to get the first value in vector y.
In Addition,
t=#(x) x+1
y(t(0))
should work.

Matlab exercise: i just don't get it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given the signals:
f1[n] = sinc[n] {1[n+5]-1[n-5]}
f2[n] = 1-rect[n]
f3[n] = 1[n]-1[n-5]
write a programm in matlab in which you will check the following proprieties:
1)sinc[n]:=sin(phi*n)/phi*n;
2)(f1*f2)[n] = (f2*f1)[n];
3)f1[n]*{ f2[n] + f3[n] } = f1[n]*f2[n] + f1[n]*f3[n];
4)(f1*delta)[n] = (delta*f1)[n] = f1[n];
I'm really really grateful for any tips/ideal on how to solve this problem. :)
sinc[n]:=sin(phi*n)/phi*n;
That certainly isn't Matlab syntax, and the ; at the end makes it not look much like a question either. Anyway, you have two options. Either plot the functions to visually assess equivalence or else check the vectors. I'll demonstrate with this one, then you can try for all the others.
Firstly you need to make a sample n vector which will be your domain over which to test equivalence (i.e. the x values of your plot). I'm going to arbitrarily choose:
n = -10:0.01:10;
Also I'm going to assuming by phi you actually meant pi based on the Matlab definition of sinc: http://www.mathworks.com/help/signal/ref/sinc.html
So now we have to functions:
a = sinc(n);
b = sin(n)./n;
a and b are now vectors with a corresponding "y" value for each element of n. You'll also notice I used a . before the /, this means element wise divide i.e. divide each element by each corresponding element rather than matrix division which is inversion followed by matrix multiplication.
Now lets plot them:
plot(n, a, n, b, 'r')
and finally to check numerical equivalence we could do this:
all(a == b)
But (and this is probably a bit out of scope for your question but important to know) you should actually never check for absolute equivalence of floating point numbers like that as you get precision errors due to different truncations in the inner calculations (because of how your computer stores floating point numbers). So instead it is good practice to rather check that the difference between the two numbers is less than some tiny threshold.
all((a - b) < 0.000001)
I'll leave the rest up to you

How many elements the output matrix should have in this code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have written the following MATLAB code and as I initialized the matrix stab1, I think this matrix should have 4991 elements at the end, but when I run the code, stab1 has 801445 elements at the end. Is the problem with my code?
stab1=zeros(1,4991);
k=0;
for ii=-0.6:0.01:-.3
m=0;
for jj=0:0.01:1.6
m=m+1;
if .... (some condition)
stab1(k*161+m)=1;
end
k=k+1;
end
end
You have put the k=k+1 at a wrong place. It is the correct code:
stab1=zeros(1,4991);
k=0;
for ii=-0.6:0.01:-.3
m=0;
for jj=0:0.01:1.6
m=m+1;
if .... (some condition)
stab1(k*161+m)=1;
end
end
k=k+1;
end
Now it has 4991 elements.