1. ## Matlab questions

Hi there,

I'm stuck on a few questions regarding Matlab programming and would be very appreciative if somebody could help and teach as I'm quite new to it.

1) Given the following operations in Matlab:

>> vec = [6 2 4 1 5 3];
>> idx1 = [0 0 1 1 0 0];
>> idx2 = idx1 == 1;

a) write the values in idx2
b) what is the difference between idx1 and idx2?
c) what is the result of the following assignments? Explain why.
A, >> vec(idx1)
B, >> vec(idx2)

2) A Matlab function requires that an error be generated if an input vector, named vec, contains any element with a value equal to zero. Implement compactly. You may assume the input has already been checked to determine if it is a vector and that it is numeric.

3) There are problems with the following Matlab code.

function vout process(vin, sp, t, pos)
error(nargchk(0,3,nargin))

What is wrong with this code? Write correct version.

Would be very appreciative if somebody could help me and also point me in the direction of Matlab tutorials/help/function writing.

Regards,

2. Originally Posted by rel85
Hi there,

I'm stuck on a few questions regarding Matlab programming and would be very appreciative if somebody could help and teach as I'm quite new to it.

1) Given the following operations in Matlab:

>> vec = [6 2 4 1 5 3];
>> idx1 = [0 0 1 1 0 0];
>> idx2 = idx1 == 1;

a) write the values in idx2
b) what is the difference between idx1 and idx2?
c) what is the result of the following assignments? Explain why.
A, >> vec(idx1)
B, >> vec(idx2)
Type the commands into Matlab and see what you get.

Look at the workspace panel of the Matlab console to see what the difference is between idx1 and idx2 (I will tell you idx2 is a logical array so vec(idx2) is the array of elements of vec for which idx2 is true.

CB

3. Originally Posted by CaptainBlack
Type the commands into Matlab and see what you get.

Look at the workspace panel of the Matlab console to see what the difference is between idx1 and idx2 (I will tell you idx2 is a logical array so vec(idx2) is the array of elements of vec for which idx2 is true.

CB
Thanks CaptainBlack, I think I've got most of them now... Any idea as to what the last one is though?

Also:

(c) Write a Matlab function to count the number of positive values in an
input array.
(d) Write a Matlab function that returns
true if a matrix contains only
non-zero elements.
(e) Write a Matlab function that returns
true is an input argument is not
a numerical array.
(f) There are problems with the following Matlab code.

ans = 5 + 10;
6 + 1;
% Must loop for exactly 15 elements (use ans)
for k = 0:1:ans-1
vec[k] = k;
What is wrong with this code?

Regards,

4. Originally Posted by rel85
Thanks CaptainBlack, I think I've got most of them now... Any idea as to what the last one is though?
I am not presently at a machine with Matlab installed so can't check, but:

Code:
function vout process(vin, sp, t, pos)
error(nargchk(0,3,nargin))
is missing an "=" between vout and process, also vout seems to be unassigned.

I cant check error(nargchk(0,3,nargin)) because I would need the matlab help system.

type "help error" and "help nargchk" at the command prompt.

CB