Installing a loop for more efficient script
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Rik
2022-4-12
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!