• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

easy matlab ?

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!!!!
 
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
 

Bastian Gross

German Mathquant
Oh, my bad, my bad!
:smt024
 
Top