Loop data in both x and y direction, and do linear fit of each y at each x

I have two matrice. matrix 1 (time points) is 96 by 1 (row is time); matrix 2 is 96 by 150 (attached). matrix 2 is the readout MSD data points at each time point (row) of 150 individual particle (column). I want to fit each MSD data of each particle at increased time point to y=ax+b and so on until the last time point, to get the fitted slope (a). In this way, I can obtain the slope of each particle at increased time points and study their diffusion behavior.
For example: time point 1 has one MSD readout, time point 2 has two MSD readout, time point 3 has three MSD readout. Each of them will generate an x,y line and I wanna find out the fitted slope of the x,y line to y=ax+b.
The matrix 1 can be written as:
time = 15:15:1440; %matrix 1
The result is ideally to be a slope matrix (96 by 150). I have very limited clue on how to do it, appreaciate if anyone know how to do this.

5 Comments

Hi Yang,

I will help you guide to resolve your problem. First, define the two matrices matrix1 and matrix2 with random values for demonstration purposes, initialize a slope_matrix of size 96x150 to store the slopes. Then, use nested loops, we iterate over each time point and each data point column in matrix 2. For each iteration, you need to fit a linear model using fitlm to find the slope (a) for the data points at that time point. Then, the slope value is extracted from the model and stored in the corresponding position in the slope_matrix. Finally, the slope_matrix containing all the slopes is displayed. I just ran this code in Matlab to obtain a slope matrix (96x150) that represents the slopes of the linear fits for each data point at every time point. Please see attached results.

If you have further questions, please let me know.

Hi Umar, thank you for your response. It seems like the slopes are all the same for all time point data, which is contradict to my expectations since each pair of matrix 1 and matrix 2 data are different, so the slope from fitted line will also be different.
Hi Yang,
To address your issue, it was an example to demonstrate what you mentioned but now it is contradicting with your expectations, so what exactly are your expectations, if you could be more specific and provide more details then I can provide tailored solution based on your specific needs.
Hi Yang,
Please see response from @Matt J, is this the solution you are seeking regarding your recent comment.
Thanks @Matt J for your contribution and sharing your knowledge, highly appreciated.
Hi Umar,
I upload actual data point called 'MSDdata' to be more specific. Each row is the readout MSD data at different time points, time interval is 15 - row 1 is the MSD readout at time 15, row 2 is the readout at time 30 and so on.
Row 1 and row 2 MSD data will generate a connected curve with respect to their time points, and this curve will be fitted to y=ax+b to obtain a fitted slope. Due to the data variation, this generated curve will have different slopes over time. I would like to fit y=ax+b to each indidivual particles at increased time points til the end, and continue for all particles.

Sign in to comment.

 Accepted Answer

t = 15:15:1440; %time
MSD=readmatrix('MSDdata.xlsx');
A=t.'.^[1,0];
slopes=nan(size(MSD));
for i=1:height(A)
tmp=A(1:i,:)\MSD(1:i,:);
slopes(i,:)=tmp(1,:);
end
whos slopes
Name Size Bytes Class Attributes slopes 96x150 115200 double

4 Comments

Hi Yang,
It calculates the element-wise exponentiation of the transposed matrix t with the values 1 and 0. So, t.' transposes the matrix t, switching its rows and columns., .^ denotes element-wise exponentiation and [1,0] is a 1x2 matrix with values 1 and 0. Hope, @Matt J can shed more guidance.
Yes, Umar is right. That's what it does.

Sign in to comment.

More Answers (1)

Perhaps this is what you want.
matrix1=rand(96,1); matrix2=rand(96,150);
[Xslope,Tslope]=gradient(matrix2,1:150,matrix1);
whos Xslope Tslope
Name Size Bytes Class Attributes Tslope 96x150 115200 double Xslope 96x150 115200 double

1 Comment

Hi Matt, thank you for your response. I updated how the data actually are. I want to fit matrix2 (now called MSD data) to increased time points, not the individual time point they are at. For example, MSD data at time point 5 will generate a x,y line with MSD data from time point 1 to 5 at increased time interval (15). So I want to fit this x,y line at increased time points to y=ax+b individually.

Sign in to comment.

Categories

Products

Release

R2023a

Asked:

on 11 Jul 2024

Commented:

on 21 Jul 2024

Community Treasure Hunt

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

Start Hunting!