how to enter equation into matrix column and refer to adjacent column.

조회 수: 2 (최근 30일)
Emily Pendleton
Emily Pendleton 2017년 12월 15일
편집: Matthew 2017년 12월 15일
Hi all. I am a Matlab beginner trying to find full width half max. Below is my code. I start with my .csv file, identify x- and y-values, fit a gaussian curve, and find half max. Next, I added x-values at evenly spaced small increments to a column in a matrix (defined as A). I want the second column (B) to find the y-values at the given x-values using the equation found with the gaussian fit (in other words, the x-value at a given row will be used in the equation to find the y-value in the adjacent column). I am stuck on the for loop here. Then, I think I can use the "find" command to find my 2 x-values to find the full width, but this may need some help too. Thank you for your help!
data = load ('Practice.csv');
datax = data(:,2);
datay = data(:,5);
f = fit(datax, datay,'gauss1')
plot(f,datax,datay)
yfitted = feval(f,datax);
hold on
[ypk] = findpeaks(yfitted)
HalfMax = 0.5*ypk
Matrix = zeros(2,1000);
A = Matrix(1,:);
for A =1:1000;
x1= 0;
x2= 1.1;
n = 1000;
A = linspace(x1, x2, n);
end;
B = Matrix(2,:); %%this is where I need help with a for loop please!
index1 = find(B <= HalfMax, 1, 'first');
index2 = find(B >= HalfMax, 1, 'last')
fwhm = index2-index1
  댓글 수: 3
Emily Pendleton
Emily Pendleton 2017년 12월 15일
With the first column in the matrix (1000 rows), I am just trying to give values to x at small, evenly spaced increment. Here, my first 5 rows of A (x-values) are: 0, 0.0011, 0.0022, 0.0033, 0.0044, 0.0055. This continues to my last 5 rows: 1.0956, 1.0967, 1.0978, 1.0989, 1.1000. I'm sorry if this code is redundant and the loop is not needed. I am attempting to have evenly spaced increases in x-value to find FWHM; is there a better way to increment x-values? Even if my code is repetitive, does my question make sense?
Walter Roberson
Walter Roberson 2017년 12월 15일
Perhaps you just want
A = linspace(0, 1.1, 1000);
Matrix(1,:) = A;

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

채택된 답변

Matthew
Matthew 2017년 12월 15일
If I understand what you're attempting to do, you can replace the 'A' for loop with one line
A = linspace(0, 1.1, 1000);
B can then be calculated as
B= feval(f,A);
  댓글 수: 2
Emily Pendleton
Emily Pendleton 2017년 12월 15일
Yes. That worked for finding the correspoinding y-values. This line is now wrong and returns a value of 1.
index1 = find(B <= HalfMax, 1, 'first')
I'm just trying to locate the row at which B is <= Halfmax. I can then use that location to find the value of A in the same row. I will do this for when B <= Halfmax too and find the difference between the x-values. Any suggestions on how to get this done?
Matthew
Matthew 2017년 12월 15일
편집: Matthew 2017년 12월 15일
You probably want to reverse the sign of your inequality.
i.e.
index1 = find(B >= HalfMax, 1, 'first')
The current code will most likely always return 1 because the first point of B is the first point less than the HalfMax.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by