Question about columns and multiplying within new columns

1 view (last 30 days)
I have a problem which is slightly complicated to explain but bear with me. I recently was given data on the time and strain of a large number of particles(1500) but all the data is represented in two columns (One for time and one for strain). There are different numbers of data rows representing each particle (particle 1 may have 150 rows of data while particle 2 may have 231 rows of data). Each particle has a break of roughly 3 rows that appears as 'NaN' ( I managed to convert the NaN to zero with '(isnan(temps))=0;').
Currently I am trying to manipulate the data to create new columns that involve different values based on equations. The second step is to graph these values. So for example I am using an equation to read stress accumulation. In Excel it roughly translates into this
for i=2:N temps(i+1,8)=temps(i,8)+temps(i+1,7); end
This takes values from the previous stress accumulation and adds it to the current stress at that particular time (thus creating the new stress accumulation value for column 8). The problem I have is that I need a method of allowing the program to distinguish between different particles . All particles are on the same column so the stress is just constantly added. Any suggestions?

Answers (1)

Chad Greene
Chad Greene on 20 Jul 2017
Hi Owen,
I think you can do this without a loop. To take the cumulative sum, there's a built-in Matlab function called cumsum. However, if you want to reset the counter at each occurrence of NaN, use Brett Shoelson's nancumsum. It's quite brilliant. Here's an example. For this column a:
a = [1 1 1 1 nan 1 1 1 nan 1 1]'
a =
1.00
1.00
1.00
1.00
NaN
1.00
1.00
1.00
NaN
1.00
1.00
Count up all the elements as a cumulative sum, and reset the counter at every NaN:
nancumsum(a,1,4)
ans =
1.00
2.00
3.00
4.00
NaN
1.00
2.00
3.00
NaN
1.00
2.00

Categories

Find more on Stress and Strain in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!