Create array of results for summation where limits are not the same

2 views (last 30 days)
I need to perform a summation for results obtained from a simulation of different ions striking a target. There are three columns on my data file, the first column Ni = ion number and goes from 0 to 10,000 while the other two columns are data for each ion. The total number of rows in an individual file are over 70,000 which in my code is supposed to be k. There are a number k of rows for each ion containing data, and each ion has a different number of k, meaning the code should be able to read all lines of Ni(k:1) and be able to tell when the summation for a specific ion, let's say ion 1 is done, then store that value and move onto the next ion until the file is over. Ideally, I should get a value of k = rows in data file, i= number of ions = 10,000, and an array of values sum_total containing the summation for each ion with length i. Instead I get i = 10,000, k=2 and just one value for sum_total which only corresponds to the 1st value of the entire dataset. How can I properly approach this? I've tried with a for loop where k=1:n as well but it still gives me the wrong answer. Any help is greatly appreciated and thanks in advance.
The .txt looks like this
clear
clc
%Import the .txt files and convert into a 3xlength matrix where [1] = Ni,
%[2]= depth, and [3]=de_dx_e
data=readmatrix("C:\Users\hp\Desktop\NASA_JANUS\IIEE_XeOnW\EXYZ0.301.txt"); %reads data from the .txt file in Matlab directory
%Variables stored as an array from matrix "data"
Ni=data(:,1); %Ion number
depth=data(:,2); % x depth in units of Armstrongs
de_dx_e=data(:,3); % Electronic stopping power in eV/A
n=length(data) %number of iterations for while loop
%Iteration for each ion
k=1;
i=1;
sum_total=zeros(10000:1);
while i<10000
sum_ion=0;
while Ni(k:1)==i;
sum_ion=sum_ion+((de_dx_e(k:1)).*exp((-depth(k:1))/11));
k=k+1
end
sum_total(i:1)=sum_ion
i=i+1
end

Accepted Answer

Steven Lord
Steven Lord on 1 Jul 2022
I don't have your data so I can't run the code, but something along these lines should work. I use groupsummary to summarize (in this case using sum) the data for each group.
%Import the .txt files and convert into a 3xlength matrix where [1] = Ni,
%[2]= depth, and [3]=de_dx_e
data=readmatrix("C:\Users\hp\Desktop\NASA_JANUS\IIEE_XeOnW\EXYZ0.301.txt"); %reads data from the .txt file in Matlab directory
%Variables stored as an array from matrix "data"
Ni=data(:,1); %Ion number
depth=data(:,2); % x depth in units of Armstrongs
de_dx_e=data(:,3); % Electronic stopping power in eV/A
% Compute the term involving depth and de_dx_e for each value in one
% calculation
term = de_dx_e.*exp(-depth/11);
% Now summarize the term data for each bin, represented by the same value
% in rows of Ni.
sum_ion = groupsummary(term, Ni, @sum);
I would like to call out something else from your code:
sum_total=zeros(10000:1);
This doesn't do what I suspect you think it does. I think you expect this to create a 10000-by-1 vector with all its elements equal to 0. It doesn't. The expression 10000:1 returns an empty matrix zeros(1, 0) since you can't get from 10000 to 1 by taking steps of length 1. When called with zeros(1, 0) as input the zeros function returns the empty 0-by-0 matrix [].
To create a 10000-by-1 vector you'd use:
sum_total = zeros(10000, 1);
But you don't need to do that in this case, since groupsummary should do the heavy lifting.
  2 Comments
Jorge Fernandez
Jorge Fernandez on 1 Jul 2022
Steven,
Thank you so much for the help. Your code runs perfectly fine and I get similar results to those obtained analytically by integrating. I had no idea about the existence of the groupsummary function and will definitely use it from now on when working with lists with many data. Also, thanks for your remark when creating the zeros vector, I had a typo and was wondering why I wasn't creating an array when I was simply indexing.
Again, thank you so much for the help,
Jorge

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!