Previous in Forum: electrical schematics   Next in Forum: Lost Windows Password
Close
Close
Close
4 comments
Rate Comments: Nested
Member

Join Date: Mar 2008
Posts: 5

Multiple Vector

04/17/2008 10:24 PM

hallo, i have a problems regarding matlab programming. i cannot execute multiple vector values.for example:

Va=[0;3;3;0]

Vb=[3;3;0;0;]

for k:4;

V1=(Va*7)+(Vb*8);

%i want the vout produces 4 outputs based on the 4 values of Va and Vb.

%VIN is the function created.

Vout=VIN(V1,Vtn,Vgs,Vdd);

%how to create the Vout the stairs waveform based on the Vout.

Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.
Guru

Join Date: Sep 2006
Posts: 4513
Good Answers: 88
#1

Re: multiple vector

04/18/2008 2:16 AM

Hello Redgurl,

Matlab is finicky about syntax and so may gag at the lack of whitespace following the semicolons in your vectors. You may also wish to remove the trailing semicolon in vector Vb. Also try writing Va and Vb as row vectors (if for no other reason than that they're a bit easier on the eye).

Hence:

Va = [ 0 3 3 0 ]
Vb = [ 3 3 0 0 ]

Create a result vector having the same size as our operand vectors. N is the number of elements in Va (Vb would do just as well since they're the same size).

N = max(size(Va));
V1 = zeros(N,1);

For each pair of elements Va(k) and Vb(k), compute a result and place the result into vector V1 at element k.

for k = 1:N,
V1(k) = 7*Va(k) + 8*Vb(k);
end

---------------------------------------

Now let's create a simple function, 'foo', that does the same thing (feel free to call 'foo' something else). Also note that comment delimiters '%' are not present. CR4's editor doesn't preserve the user's format, and so we would probably find our delimiters all over the map.

function [X] = foo(A,B)

N = max(size(A));
X = zeros(N);

for i = 1:N,
X(i) = 7*Va(i) + 8*Vb(i);
end

End function foo(A,B)

------------------------------------------------

Example of using function foo(A,B):

Va = [ 0 3 3 0 ]
Vb = [ 3 3 0 0 ]

Z = foo(Va,Vb);

Hope this helps.

-e

Register to Reply
Guru

Join Date: Sep 2006
Posts: 4513
Good Answers: 88
#2

Re: multiple vector

04/18/2008 2:35 AM

Erratum:

In function foo(A,B) the line:

X(i) = 7*Va(i) + 8*Vb(i);

should read:

X(i) = 7*A(i) + 8*B(i);

Register to Reply
Active Contributor

Join Date: Apr 2007
Posts: 24
#3

Re: Multiple Vector

04/19/2008 7:05 PM

Matlab understands vectors so most of the time one doesn't need to use the "for..." construct. The following sequence will do it:

Va = [0 3 3 0];

Vb = [3 3 0 0];

V1 = 8*Va + 7*Vb;

% If you prefer column vectors instead of row:

Vout = V1';

% You may be aware of the following:

Va*Vb' % yields:

answer 9

% while

Va.*Vb % yields:

answer [0 9 0 0]

% once again, one doesn't need the "for..."

% cheers

Register to Reply
Guru

Join Date: Sep 2006
Posts: 4513
Good Answers: 88
#4
In reply to #3

Re: Multiple Vector

04/19/2008 8:31 PM

Thanks, Daniel. This is good to know.

Register to Reply
Register to Reply 4 comments

Previous in Forum: electrical schematics   Next in Forum: Lost Windows Password

Advertisement