Clear Filters
Clear Filters

How to create a multidimensional array of fixed dimensions?

8 views (last 30 days)
I would like to create an array in 4 dimensions. Each dimension has a fixed size.
x =0 to 200 -->step 1
y =-25 to 25 --> step 0.5
z = 0 to 180 --> step 1
h = -10 to 10 --> step 0.1
The idea behind is to save one value on a specifc position.
For example if I have an input array [30,-0.5,100,2 ] = 21
At that location I want to save the value.
At a specific amount I would like to read the values inside this array.
Hope you can help me! :)

Accepted Answer

Walter Roberson
Walter Roberson on 9 May 2020
xmin = 0; xmax = 200; xincr = 1;
ymin = -25; ymax = 25; yincr = 0.5;
zmin = 0; zmax = 180; zincr = 1;
hmin = -10; hmax = 10; hincr = 0.1;
xvec = xmin : xincr : xmax;
yvec = ymin : yincr : ymax;
zvec = zmin : zincr : zmax;
hvec = hmin : hincr : hmax;
x2xidx = @(xval) round((xval - xmin)/xincr) + 1;
y2yidx = @(yval) round((yval - ymin)/yincr) + 1;
z2zidx = @(zval) round((zval - zmin)/zincr) + 1;
h2hidx = @(hval) round((hval - hmin)/hincr) + 1;
nx = length(xvec);
ny = length(yvec);
nz = length(zvec);
nh = length(hvec);
M = zeros(nx, ny, nz, nh);
%example of use
M(x2xidx(17), y2yidx(-6.5), z2zidx(93), h2hidx(-4) ) = 1;
[XIDX, YIDX, ZIDX, HIDX] = ind2sub(size(M), find(M));
disp([XIDX, YIDX, ZIDX, HIDX])
disp([xvec(XIDX), yvec(YIDX), zvec(ZIDX), hvec(HIDX)])
You can store by index or you can use the helper functions to convert numeric value to index.
  7 Comments
Walter Roberson
Walter Roberson on 9 May 2020
xmin = 0; xmax = 10; xincr = 1;
ymin = -5; ymax = 5; yincr = 0.5;
zmin = 0; zmax = 30; zincr = 1;
hmin = -10; hmax = 10; hincr = 0.5;
for x = xmin:xincr:xmax
for y = ymin:yincr:ymax
for z = zmin:zincr:zmax
for h = hmin:hincr:hmax
disp([x y z h])
end
end
end
end
or
xmin = 0; xmax = 10; xincr = 1;
ymin = -5; ymax = 5; yincr = 0.5;
zmin = 0; zmax = 30; zincr = 1;
hmin = -10; hmax = 10; hincr = 0.5;
xvec = xmin : xincr : xmax;
yvec = ymin : yincr : ymax;
zvec = zmin : zincr : zmax;
hvec = hmin : hincr : hmax;
nx = length(xvec);
ny = length(yvec);
nz = length(zvec);
nh = length(hvec);
for xidx = 1 : nx
x = xvec(xidx);
for yidx = 1 : ny
y = yvec(yidx);
for zidx = 1 : nz
z = zvec(zidx);
for hidx = 1 : nh
h = hvec(hidx);
disp([x y z h]);
end
end
end
end
If you are going to loop over values instead of doing vectorized calculations, then it is typically much better to loop over the indices, in order to permit you to store at appropriate output locations, such as
output(xidx, yidx, zidx, hidx) = x.^2 - y.^3/3 - (x.*z).^h;
Mariana
Mariana on 14 May 2020
What if I have a variable step per vector. How will you suggest to find the index?
rmin = 0; rmax = 90; rincr = 0.5;
r2min = 91; r2max = 180; r2incr = 1;
r3min =181.5; r3max = 250 ; r3incr = 1.5;
rvec = rmin : rincr : rmax;
r2vec = r2min : r2incr : r2max;
r3vec = r3min : r3incr : r3max;
relx = single([rvec,r2vec,r3vec]);

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 8 May 2020
There's no such thing as the -25th column in an array in MATLAB, nor is there such a thing as the 1.5th column.
You can make an array with separate coordinates that you can use like indices (though as usual, be careful when performing exact equality comparisons with == on floating-point numbers. What I wrote below is safe because all the numbers in x and y can be represented exactly in double precision.)
x = (-5:5).';
y = (-3:0.5:3);
z = x.^2 + y.^3;
z(x == -4, y == 1.5) % (-4)^2+(1.5)^3
If you tell us a little more detail about what you're trying to do with this array we may be able to offer alternate suggestions.
  1 Comment
Mariana
Mariana on 9 May 2020
This is the kind of the idea that I would like to do. Just to save a value at a specific space of this array. The array has 8 dimensions or 8 indices. How would you suggest to build and add values to this storage array?

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!