# easy matlab ?

#### examinator

Heres the the deal I have a program I'm writing in matlab for a class and thus far Ive made a function that saves values in an array.
array 1: x = [a1 a2 a3]
array 2: y = [b1 b2 b3]
the size of the array will be known, I need to use another function(already made) call it f to do the following: f(a1,b1), f(a2,b2) etc. I know how to do the iterating and loops and stuff but I don't know how to access the particular elements of the arrays. How do you access specific elements of arrays?

#### Bastian Gross

##### German Mathquant
C++:
``````function [y] = f(x1,x2)
y = 2*x1.^2 + 3*x2;
%End Function!!!!!!!!!!! This must be a seperate m-file : "f.m"

%array example
%array 1: x = [a1 a2 a3]
%array 2: y = [b1 b2 b3]
%
a1 = 1;
a2 = 2;
a3 = 1.5;
b1 = -1;
b2 = 0.5;
b3 = pi;

x = [a1 a2 a3];
y = [b1 b2 b3];

%and now insert this in the Command Window
y = f(x,y)   % estimate y =   -1.0000    9.5000   13.9248
%resp.
y = f(x(1),y(1))
y = f(x(2),y(2))
y = f(x(3),y(3))``````

This should work!!!!

#### atito

Nop!
you have to change the last three lines:
z = f(x(1),y(1))
z = f(x(2),y(2))
z = f(x(3),y(3))

to have the 'expected' behavior

:smt024

Replies
0
Views
641
Replies
0
Views
2K
Replies
2
Views
1K
Replies
2
Views
8K
Replies
1
Views
3K