Effacer les filtres
Effacer les filtres

How can I write a loop that performs a function to each element in a matrix / array?

3 vues (au cours des 30 derniers jours)
Jonathan Miles
Jonathan Miles le 11 Déc 2020
Commenté : Ive J le 12 Déc 2020
How can I create a code that can preforms a function to each value in a matrix?
Below is Reference:
function yi = TableLook(x, y, xx)
n = length(x);
if xx < x(1) || xx > x(n)
error('Interpolation outside range')
end
% sequential search
i = 1;
while(1)
if xx <= x(i + 1), break, end
i = i + 1;
end
% linear interpolation
yi = y(i) + (y(i+1)-y(i))/(x(i+1)-x(i))*(xx-x(i));
x is equal to e. and e=linspace(0,1,1668)
y is equal to X. *I have predetermined the X values on my end
I am trying to create a code that calls Table look and inputs a value from 0-1.
For example I would like my code to use this arrangment of values [0.01 0.02 0.03].
I am new to MATLAB and have been struggling with this. Please help.
  2 commentaires
SHIVAM KUMAR
SHIVAM KUMAR le 11 Déc 2020
%You can call the function in for loop
%I don't get what you are trying to do just some guess
input=[0.01 0.02 0.03];
for i=1:length(x)
for j=1:length(y)
result(i,j)=TableLook(x,table(i,j), input);
end
end
Ive J
Ive J le 12 Déc 2020
If you intend to apply your function to elements of an array vector, you can use arrayfun
xx = 1;
yi = arrayfun(@(x, y)TableLook(x, y, xx), linspace(0, 1, 1668), linspace(0, 1, 1668));

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by