Creating a cell array within a nested loop

I am trying to fill cell arrays with values within a loop, but I am not entirely sure how to do this for what I need.
'zdr_bin' and 'ht_bin' below are subsets of bigger arrays, and each of these *_bin variables can sometimes be 0, or sometimes 30-50 entries, etc, depending on the radar scan (these loops are nested within a larger loop). Ultimately, I need these entries at each i,j pair in a 3D variable. Since it can vary in the 3rd dimension (which is all values in zdr_bin or ht_bin), I was thinking a cell array makes sense. I am not sure how to index this because this last part fails, any thoughts are appreciated!
Attached below is the small loop where my code is failing.
%loop through all model grid points to create bins around each model grid point
%tic
for i=1:2:size(lon,1)
for j=1:2:size(lon,2)
%calculate distance of all observations from model grid point
mod_dist = deg2km(distance(lat(i,j),lon(i,j),obs_lat,obs_lon));
%take observations within the distance threshold, and therefore in the bin
bin_ind = find(mod_dist<dist_thres);
%add bin values to a variable
zdr_bin=zdr(bin_ind); ht_bin=obs_h(bin_ind);
%if variable is not filled, go to next iteration
if length(zdr_bin)<1; continue; end;
%add to a cell array
zdr2{:}(i,j)=zdr(bin_ind); ht2{:}(i,j)=obs_h(bin_ind);
end
end

Answers (1)

You did not mention the error message you get, but the line where you are trying to add new entry into cell array contains an incorrect indexing. Namely, zdr2{:} produces a comma-separated list of all values in the cell array, so it is not possible to subsequently index into it with (i,j). You have to put the cell indices into curly braces like this: zdr2{i,j} and directly access the appropriate cell, when storing a single cell value. Note also that zdr2(i,j) is a cell, whereas zdr2{i,j} is the contents o fthe cell...

4 Comments

Sorry for not including that earlier:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right
side.
Error in so_zdr_modelgridpt (line 185)
zdr2{:}(i,j)=zdr(bin_ind); ht2{:}(i,j)=obs_h(bin_ind);
I know it is incorrect, but if I use zdr2{i,j}, then I cannot use this in the outer loop. Ideally, I would like the cell array to be a 3D variable, but in every outer loop iteration, zdr2's 3rd dimension would have entries added to it.
As always, there are several options available:
  • In case all your data are a single type (presumably numeric), you can indeed use a 3D array, which would be only partially filled in the third dimension (rest would be just zeros or NaN). The number of elements in the 3rd dimension may be stored in a separate 2D array, for convenience. You can then get a single element using: zdr2(i,j,k).
  • Using a cell array is also possible, and you can index into it to the same single element using zdr2{i,j}(k). This option gives you the benefit that your data has no void spaces and you may e.g. use the size functon to see the size of every vector in a single cell like this: N=size(zdr2{i,j})
In either case, remember to preallocate the aray for speed.
If this answers your question, please accept the answer.
I still get the same error with the second option. I cannot really preallocate the array because I will not know how big it will be until 'bin_ind' is found in each iteration k.
By "second option" you mean a 3D array? I understand that you may be unable to preallocate the third dimension in this case, but that is not really a problem in MATLAB, it would only cause some slow-down.

Sign in to comment.

Asked:

KCE
on 5 Oct 2022

Commented:

on 10 Oct 2022

Community Treasure Hunt

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

Start Hunting!