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

2 views (last 30 days)
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 Comments
SHIVAM KUMAR
SHIVAM KUMAR on 11 Dec 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 on 12 Dec 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));

Sign in to comment.

Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!