필터 지우기
필터 지우기

How to evaluate function matrix in for loop

조회 수: 3 (최근 30일)
Alexa Shumaker
Alexa Shumaker 2019년 4월 17일
답변: Walter Roberson 2019년 4월 17일
The text file is 8 X178
First four column are the y axis values do not change
last 4 rows are the x values and im converting them in a function Wavelength
I am having an issue with my function. I am trying to pass my function a 4X178 matrix and evaluate is at each point for each row and save it into another row vector.
Text file provided
clc
clear
type TestPatient.txt
PI = dlmread('TestPatient.txt');
[n,m] = size(PI);
PW = [PI(5,:); PI(6,:); PI(7,:); PI(8,:)];
DarkWave = ones(4,m);
AmbientWave = ones(4,m); SunWave = ones(4,m); PatientWave = ones(4,m);
for i = 1:4
% dark
%DarkWave(i) = Wavelength(D1(i,m), 4,m);
% ambient light
%AmbientWave(i) = Wavelength(A1(i,m), 4,m);
% sun light
%SunWave(i) = Wavelength (SN1(i,m),4,m);
%Patient Data
PatientWave(i) = Wavelength (PW(i),4,m);
end %end for loop
%Patient Plot
subplot(2,2,4)
plot(PI(1,:), PatientWave(1), 'b',PI(2,:), PatientWave(2), 'r',PI(3,:), PatientWave(3), 'g',PI(4,:), PatientWave(4), 'k')
title('Patient Readings')
legend('White Lights', 'IR Lights', 'Near IR Lights', 'UV Lights')
xlabel('Wavelength')
ylabel('Intensity')
%%
function f = Wavelength(x, n,m)
%n = size of rows
%m = number of columns
A0 = 3.137749190*10^2;
B1 = 2.712866004;
B2 = -1.458600858*10^-3;
B3 = -4.854192924*10^-6;
B4 = -1.392291111*10^-9;
B5 = 1.937582620*10^-11;
%x = pixel data
WL = [n,m] ;
for i = 1: n
% for loop for column
for j = 1: m
% for loop for rows
%WL = wavelengh conversion of pixels to Wavelength
WL(i) = A0 + (B1*x(i,j))+ (B2*x(i,j)^2) + (B3*x(i,j)^3)+(B4*x(i,j)^4) + (B5*x(i,j)^5);
end % end for loop
end % end for loop
f= WL;
end % end of function
  댓글 수: 3
Alexa Shumaker
Alexa Shumaker 2019년 4월 17일
I was trying to store the conversion in WL(i) for each iteration but is that wrong and I don't know how to fix it because i want to convert the last 4 rows of the text file and i want to pass the row and its conversions within each column
Alexa Shumaker
Alexa Shumaker 2019년 4월 17일
Now i am getting this error Index in position 2 exceeds array
bounds (must not exceed 1).
What does that mean?
function f = Wavelength(x,n,m)
%n = size of rows
%m = number of columns
A0 = 3.137749190*10^2;
B1 = 2.712866004;
B2 = -1.458600858*10^-3;
B3 = -4.854192924*10^-6;
B4 = -1.392291111*10^-9;
B5 = 1.937582620*10^-11;
%x = pixel data
WL = [n,m] ;
% for loop for column
for j = 1: m
% for loop for rows
%WL = wavelengh conversion of pixels to Wavelength
WL(n,j) = A0 + (B1*x(n,j))+ (B2*x(n,j)^2) + (B3*x(n,j)^3)+(B4*x(n,j)^4) + (B5*x(n,j)^5);
end % end for loop
f= WL;
end % end of function

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 4월 17일
function f = Wavelength(x)
A0 = 3.137749190*10^2;
B1 = 2.712866004;
B2 = -1.458600858*10^-3;
B3 = -4.854192924*10^-6;
B4 = -1.392291111*10^-9;
B5 = 1.937582620*10^-11;
P = [B5 B4 B3 B2 B1 A0];
f= polyval(P, x);
end % end of function

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by