How to evaluate function matrix in for loop
Mostrar comentarios más antiguos
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 comentarios
Walter Roberson
el 17 de Abr. de 2019
You assign to WL(i) within a for j loop. The value calculated for j = 2 will overwrite the value calculated for j = 1, the j = 3 will overwrite the value calculated when j = 2, and so on.
Alexa Shumaker
el 17 de Abr. de 2019
Alexa Shumaker
el 17 de Abr. de 2019
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!