Selecting different element of a matrix with each function operation

Hi guys,
Say I have a two column 5x2 matrix A (defined as a global variable) with 0,1,2,3,4 in the first column and 2,4,6,8,10 in the second column. Say I also have a function in a separate function file that goes something like this:
function y = func(x)
global A;
y = x - A(p,n)
end
In my main Matlab file (where A is), I've set up a 'while' loop whereby func(x) continuously operates for a total of 5 times.
So my question/problem is this: for each operation of func(x), is it possible to select a different element of the matrix A. For example, for the first func(x) operation, I want it to carry out y = x - A(1,2); for the second func(x) operation, I want it to carry out y = x - A(2,2), and so on until the fifth func(x) operation completes.
Hopefully this is somewhat understandable - I've 'created' the above problem as an example designed to highlight the specific issue I'm trying to tackle, so it might not work/make sense as a whole.
Any help is greatly appreciated.

 Réponse acceptée

Hi JD,
You could pass a counter variable i to your function as such:
i = 1;
while condition
i = i+1;
end
function y = func(x, i)
global A;
y = x - A(i,2);
end

2 commentaires

that seems to have done it - thank you very much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by