필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
Jonathan Miles
Jonathan Miles 2020년 12월 11일
댓글: Ive J 2020년 12월 12일
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