Search code examples
matlabvectorcartesian-coordinates

MATLAB: Applying multiple values to each (x,y) coordinate point in a 2D Cartesian coordinate system


I have a (5x1) vector V = (V1, V2, V3, V4, V5) and would like to assign these five values to each grid point in an (x,y) coordinate system. So for instance (x,y) = (1,1) may have values of V = (1, 0.432, -5, 2, 67) and (x,y) = (2,3) may have values of V = (-43, 3.53, 0.423, -4, -0.432) assigned to it. Is there a good data structure that will be able to do this in MATLAB?


Solution

  • Same number of values everywhere

    If you want to assign the same number of values to each grid point, it is best to create a 5xXxY matrix, for example

    xy_vals = rand([5 nx ny]);
    

    where you can access the values at point (xx,yy) via

    test_vals = xy_vals(:,xx,yy);
    

    Flexible number of values at each grid point

    If you want to be flexible about the number of values per grid point, I suggest a cell-array of arrays. For example:

    xy_cell = cell([nx ny]);
    for ix = 1:nx
        for iy = 1:ny
            xy_cell{ix,iy} = randi([1 randi(10)]);
        end
    end
    

    Now, access the vaues at point (xx,yy) via

    test_vals = xy_cell{xx,yy};