Installing a loop for more efficient script

2 views (last 30 days)
Good Morning,
I have a csv file with 305 lines. This lines shows the measured intensitiy of elements. I need to correct interferences between the elements. The elements are saved in a string, called element_RL and the intensities are saved as doubles in intens_RL.
Therefore I am searching the element:
[~, int_elem_RL] = ismember('54Fe+', element_RL);
Then I am saving its intensity:
intens_int(:,1)=intens_RL(:,int_elem_RL);
And in the following I calculate the interference and safe the new intensity:
intens_RL(:,63)=intens_int(:,1)-0.028.*intens_RL(:,58);
So I am doing this for a lot of elements and have to write around 200 lines. I wanted to ask if somebody knows how I could short this or make it more efficient, for example with a loop?
Here are more lines of my code:
[~, int_elem_RL] = ismember('54Fe+', element_RL);
intens_int(:,1)=intens_RL(:,int_elem_RL);
intens_RL(:,63)=intens_int(:,1)-0.028.*intens_RL(:,58);
[~, int_elem_RL] = ismember('58Fe+', element_RL);
intens_int(:,1)=intens_RL(:,int_elem_RL);
intens_RL(:,70)=intens_int(:,1)-2.617.*intens_RL(:,73);
[~, int_elem_RL] = ismember('58Ni+', element_RL);
intens_int(:,1)=intens_RL(:,int_elem_RL);
intens_RL(:,71)=intens_int(:,1)-0.003.*intens_RL(:,66);
[~, int_elem_RL] = ismember('64Zn+', element_RL);
intens_int(:,1)=intens_RL(:,int_elem_RL);
intens_RL(:,78)=intens_int(:,1)-0.035.*intens_RL(:,73);
My probleme is, that I dont know how to install a loop, because the lines are so individual.
Maybe somebody else has an idea. I would appreciate this.
  3 Comments
Tatjana Mü
Tatjana Mü on 12 Apr 2022
The value to subtract is manually calculated. If I understand correctly, you mean the indices in the last row? For example for the explained line: 63 is the column where 54Fe is in the original file and 58 is the column of an element, which you use for correction.
Mathieu NOE
Mathieu NOE on 12 Apr 2022
hello
maybe it would be a good idea to share the CSV file as well
all the best

Sign in to comment.

Accepted Answer

Rik
Rik on 12 Apr 2022
If there is no way to determine these values, you will have to hard-code them somewhere. So you might as well do it in a cell array:
indices={...
'54Fe+',63,-0.028,58;
'58Fe+',70,-2.617,73;
'58Ni+',71,-0.003,66;
'64Zn+',78,-0.035,73};
for n=1:size(indices,1)
[~, int_elem_RL] = ismember(indices{n,1}, element_RL);
intens_int(:,1)=intens_RL(:,int_elem_RL);
intens_RL(:,indices{n,2})=intens_int(:,1)+indices{n,3}.*intens_RL(:,indices{n,4});
end

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing 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!