Questions related to saving multiple arrays in one variable

I have a variable “fi”, which stores a global grid of elevations (units: meters), and another variable “DL” that stores 26 depth levels. Below are my scripts for creating some land/sea masks:

function f1(fi, DL)
    A = [];
    for i in 1:length(DL)
        B = fi .> -DL[i];
        A = [A, B];
L0 = f1(fi, DL);

When I checked the created mat file in Matlab, I encountered multiple issues. Would anyone please help?

  1. Why does my L0{1} give me the below answer, when it should be a grid of zeros and ones? BTW, my L0{2} gives me the correct grid.
    2×1 cell array
    { 2×1 cell }
    {1440×720 logical}

  2. Why does L0 only contains two grids, instead of the 26 grids the loop produces?

Many thanks!

I think you want push!(A,B) here, but anyway it is hard to know because we can’t run your code and it is not clear what exactly you want as an output.


That solved my problem beautifully.

Thank you so much!

1 Like

@leon, fyi the f1 function could be written conveniently as a comprehension:

f1(fi, DL) = [fi .> -DL[i] for i in 1:length(DL)]
1 Like

I just tried it and it worked like a charm!

Many thanks for sharing that trick!

1 Like