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 commentaires

Arun Kumar Singh
Arun Kumar Singh le 22 Sep 2022
I have to run this code for 50 million data points then how should i fasten the loop ? Cn you help me to rewrite this code so it should work faster ?
Rik
Rik le 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
Arun Kumar Singh le 22 Sep 2022
since i am new on matlab so you need to help me to write the code.
is it possible to do all the comparision outside the loop or do we need loop for this.. just write the necessary code for this task.
The TASK: data of earch row comapres data with the next row ..(example a(i) compared with a(i+1)) and if the all three variables are same SAT1, ST1 and COMB1 and there is difference of just 1 for variable TIme1 then put these values in the other varable called Long {k} and if any one of the conditions is not true then increase k and put all the values in Long{k+1}.
Hope i am able to desribe my problem.
Torsten
Torsten le 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
Arun Kumar Singh le 22 Sep 2022
No i do not need those values when condition becomes false.

Connectez-vous pour commenter.

Réponses (1)

Torsten
Torsten le 22 Sep 2022
Modifié(e) : Torsten le 22 Sep 2022
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
I1 = 1×4 logical array
1 1 1 1
I2 = strcmp(SAT1(1:n-1),SAT1(2:n))
I2 = 1×4 logical array
0 1 1 1
I3 = strcmp(ST1(1:n-1),ST1(2:n))
I3 = 1×4 logical array
0 1 1 1
I4 = strcmp(COMB1(1:n-1),COMB1(2:n))
I4 = 1×4 logical array
0 1 1 1
I = (~I1) | (~I2) | (~I3) | (~I4)
I = 1×4 logical array
1 0 0 0
edges = [find(I == 1),n]
edges = 1×2
1 5
LONG1 = [3 10 12 4 8]
LONG1 = 1×5
3 10 12 4 8
LONG = arrayfun(@(i)LONG1(edges(i)+1:edges(i+1)-1),1:numel(edges)-1,'UniformOutput',0)
LONG = 1×1 cell array
{[10 12 4]}

5 commentaires

Rik
Rik le 22 Sep 2022
A loop will generally beat arrayfun (same goes for cellfun, unless you're talking about the legacy syntax).
Torsten
Torsten le 22 Sep 2022
Modifié(e) : Torsten le 22 Sep 2022
Do you have a better idea than using a loop or "arrayfun" to extract the parts of the array LONG1 that belong to the parts where the logical array "I" has zeros and save these parts in a cell array ?
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)
LONG = 1×1 cell array
{[10 12 4]}
Torsten
Torsten le 22 Sep 2022
Modifié(e) : Torsten le 22 Sep 2022
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
Rik le 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.

Connectez-vous pour commenter.

Catégories

Produits

Version

R2018b

Commenté :

Rik
le 22 Sep 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by