How to fasten the loop
Show older comments
for i=1:size(LAT1)-1
disp(i);
if ((Time1(i+1)-Time1(i)==1)&& (strcmp(SAT1(i),SAT1(i+1))) && (strcmp(ST1(i),ST1(i+1))) && (strcmp(COMB1(i),COMB1(i+1))) )
Long{k}(j,1)=Long1(i);
LAT{k}(j,1)=LAT1(i);
STEC{k}(j,1)=STEC1(i);
VTEC{k}(j,1)=VTEC1(i);
ELV{k}(j,1)=ELV1(i);
Time{k}(j,1)=Time1(i);
SAT{k}(j,1)=SAT1(i);
ST{k}(j,1)=ST1(i);
COMB{k}(j,1)=COMB1(i);
j=j+1;
else
k=k+1;
j=1;
end
end
5 Comments
Arun Kumar Singh
on 22 Sep 2022
Rik
on 22 Sep 2022
Since i will always be >= j, these assignments can all be done outside the loop.
The way you have written your code (no comments at all, short non-descriptive variable names) suggest to me you might not be pre-allocating these arrays, which may lead to substantial inefficiencies.
The most effective way might be to attempt all assignments as array operations. The exact code you need depends on the details of your data structure.
Arun Kumar Singh
on 22 Sep 2022
Torsten
on 22 Sep 2022
Don't you always forget to put Long1(i), LAT1(i),..., COMB1(i) in the new cell array Long{k+1}(1,1),LAT{k+1}(1,1),...,COMB{k+1}(1,1) if the if-condition is false ?
I mean: Imagine the if-condition is false for all i - then the cell arrays Long, LAT,...,COMB would be empty.
Arun Kumar Singh
on 22 Sep 2022
Answers (1)
Maybe there are faster commands than arrayfun for extracting the elements of LONG in cell arrays that correspond to sequences of zeros in the logical i array, but I couldn't find an efficient ad hoc solution for this.
Maybe MATLAB experts can help here.
TIME = [1 2 3 4 5];
SAT1 = ["a","aa","aa","aa","aa"];
ST1 = SAT1;
COMB1 = SAT1;
n = numel(TIME);
I1 = diff(TIME) == 1
I2 = strcmp(SAT1(1:n-1),SAT1(2:n))
I3 = strcmp(ST1(1:n-1),ST1(2:n))
I4 = strcmp(COMB1(1:n-1),COMB1(2:n))
I = (~I1) | (~I2) | (~I3) | (~I4)
edges = [find(I == 1),n]
LONG1 = [3 10 12 4 8]
LONG = arrayfun(@(i)LONG1(edges(i)+1:edges(i+1)-1),1:numel(edges)-1,'UniformOutput',0)
5 Comments
Rik
on 22 Sep 2022
A loop will generally beat arrayfun (same goes for cellfun, unless you're talking about the legacy syntax).
I don't have any practical advice, other than to say that a loop with pre-allocation tends to perform better than a call to arrayfun. I don't know what would beat a loop in this case.
I'm a bit tired, so I don't fully understand the code you've written. Maybe this?
I = [true false false false];
LONG1 = [3 10 12 4 8];
LONG{1} = LONG1(~I)
The contiguous parts of the array LONG1 should be extracted that belong to the contiguous parts where the I array is false.
I = [true false false true true false true]
should extract
LONG{1} = [LONG1(2),LONG1(3)]
LONG{2} = [LONG1(6)]
Rik
on 22 Sep 2022
Perhaps Jan's RunLength function will be helpful to see how to split the contiguous parts efficiently.
Otherwise, ~I should be a good start: you would only have to split that based on the run length of false, for which you could use mat2cell.
Categories
Find more on Matrix Indexing 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!