How to create a multidimensional array of fixed dimensions?
조회 수: 9 (최근 30일)
이전 댓글 표시
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! :)
댓글 수: 0
채택된 답변
Walter Roberson
2020년 5월 9일
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
Walter Roberson
2020년 5월 9일
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;
추가 답변 (1개)
Steven Lord
2020년 5월 8일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Quadratic Programming and Cone Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!