Filter löschen
Filter löschen

How to evaluate function matrix in for loop

3 Ansichten (letzte 30 Tage)
Alexa Shumaker
Alexa Shumaker am 17 Apr. 2019
Beantwortet: Walter Roberson am 17 Apr. 2019
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 Kommentare
Alexa Shumaker
Alexa Shumaker am 17 Apr. 2019
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 am 17 Apr. 2019
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Apr. 2019
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

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by