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

3 次查看(过去 30 天)
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 个评论
SHIVAM KUMAR
SHIVAM KUMAR 2020-12-11
%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 2020-12-12
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));

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by