- Please note that as an alternative you can use Vectorization which is more concise and less prone to errors. For more information you can refer to the following link: https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
- Further, you can preallocate ph and ph_id, instead of resizing them in each iteration.
- Also, consider extracting the if block utilized for the first segment check within each iteration, out of the for loop.
How to iterate through the rows of array and grab data?
2 次查看(过去 30 天)
显示 更早的评论
I have a structure of data that contains photon heights, 'track1.ph', in the ocean as well as the lat, lon, time, etc. I also have a variable, 'track1.surf_type' thats a flag that lets me know which data is in the open ocean. However, 'track1.surf_type' flags each segment instead of the individual photon heights. I don't know which photons belong to each segment, however I have a variable 'track1.seg_ph' that lets me know how many photons are in each segment. I created a for loop that iterates through the 'track1.seg_ph' to use the number of photons in each segment as an index in the 'track1.ph' array. I will post the code below. The only thing is that it takes a while for the code to run due to the amount of data thats in each file. Is there any possible way that I can make my code much more efficient and run more quickly? I will post the code and some data below.
function [ph, ph_id]=getOceanData(track)
%use code to identify ocean data
%variable track1.surf_type to find ocean data (2nd row is ocean, segement==1
%track1.seg_ph to know the photon count of each segement
%use photon count as an index to see which segment each photon belongs to
ph=[]; ph_id=[];
for i=1:length(track.seg_ph)
if i==1
l= 1:track.seg_ph(i); %get segment photon count for first segment
if l ~= 0
k=track.ph(1:track.seg_ph(i)); %get the photons assosciated with first segment
if track.surf_type(2,i) == 1 %see if segment is ocean data
ph=[ph; k];
ph_id=[ph_id; l'];
ffl=ph_id(end);
else
ffl=track.seg_ph(i);
end
else
ffl=0;
end
else
if track.seg_ph(i) ~= 0 %if statement to make sure segment isn't empty
f=ffl; %find the index for the last photon of the last segment
ffl=f+track.seg_ph(i); %add the next segment photon count to this segment to get index
l=f:ffl; %index of photons for this segment
if track.surf_type(2,i) == 1 %see if segment is ocean data
ph_id=[ph_id; l']; %save index of photons
ph_id=unique(ph_id);
%ph=[ph; track.ph(l)]; %save photons that contain ocean data
end
end
end
end
0 个评论
回答(1 个)
Amit Dhakite
2023-3-13
Hi Shayma,
There are several ways you can make your code efficient and faster:
For further information, kindly go through the following link: https://www.mathworks.com/help/matlab/matlab_prog/techniques-for-improving-performance.html
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Elementary Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!