Create array of results for summation where limits are not the same
Show older comments
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
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!