Store arrays of different size in each for loop itertaion

Hi,
In the below script, I need to store the output kkkk into a new array but in the same column. Here, size of the kkkk is different during each itertaion.
I know that cell array would solve the issue. But I need to store them in the single colum.
for i=1:4
kkkk = find(A>=sz(i,1) & A<=sz(i,1)+100);
clear kkkk
end

 Risposta accettata

kkk=cell(4,1);
for i=1:4
kkkk{i} = find(A>=sz(i,1) & A<=sz(i,1)+100);
end
kkkk=vertcat(kkkk{:});

5 Commenti

Actually, my i runs from 1 to 10000. Building cell array for this large numbers taking hug amount of processing time.
Previously I used below command, which worked perfectly and super fast. However, the only use over here is, for e.g. the first and second entries in sz are 402725 and 402925, so the below condition considers all the values fall between 402725 - 402925, but actually it should consider only 402725 - 402825. And for second case it should go from 402925 - 403025 and so on....
So is there any way to implement this in the below command?
k = find(A>=sz(1,1) & A<=sz(10000,1));
"Building cell array for this large numbers taking hug amount of processing time. "
Make sure that you preallocate the cell array correctly. Matt J's code used different variable names before the loop and within the loop, which would slow things down.
Hi Stephen,
I corrected the varibale name and preallocated the cell array correctly. Still the processing time is very large.
I corrected the varibale name and preallocated the cell array correctly. Still the processing time is very large.
If you profile the code, I think you will see that the bottleneck is coming from find(), not from anything else. It's an expensive function.

Accedi per commentare.

Più risposte (1)

Matt J
Matt J il 11 Apr 2024
Modificato: Matt J il 11 Apr 2024
b=sparse(sz(:,1)');
kkkk = nonzeros( (A(:)>=b & A(:)<=b+100).*(1:numel(A))' );

1 Commento

Hi,
This command leading to out of memory issue.
Requested 179528104x9700 (13288.7GB) array exceeds maximum array size
preference (15.4GB). This might cause MATLAB to become unresponsive.

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by