필터 지우기
필터 지우기

Initializing multidimensional array and dynamic expansion

조회 수: 62 (최근 30일)
Sandeep Parameshwara
Sandeep Parameshwara 2020년 1월 14일
편집: Stephen23 2020년 1월 15일
Hello, I am trying to initialize array 'A' with unspecified dimension and sizes. Then, I proceed to specify the size of each dimension thereby also specifying how many dimensions it has. It looks something like this.
A=[]; %This initialization is wrong, but I want to initialize it somehow
Next, I would like to specify size of dimension and also 'fill entire array with zeros' for preallocation purposes(I have not done that below)
size(A,1)=5; %This means my array A has second dimension of size 5.
size(A,2)=5; %This means my array A has third dimension of size 5.
I have a pattern after this. So I would like to create a 'for' loop like this because number of dimension of my array depends on length of some object 'rho{i}' which is again contained in cell array 'rho' of size 1 by n.
for i=3:(2+n)
size(A,i)=length(rho{i}))
end
So, after this I would like to have 5 dimensional array A(assuming I had 3 objects in my cell array rho) completely filled with zero (pre-allocated)
What I want to learn is how can I automate the indexing here? For ex, if I have a 5D array i dont want to replace all indexing by A(i,i,:,:,:). The code should be generated by just specifying number of objects in my cell array rho. Then I would like to fill my array with numbers. This is just example of 4D array, here I have hardcoded entries for all the dimension. I would like to automate this in the code. Pls note that n, d, k's are all known.
A(1,:,:,:)=[-2*d d zeros(1,n-2) -(k(1)+k(2)) k(2) zeros(1,n-2)];
for i=2:n-1
A(i,i-1,:,:)=d;
A(i,i,:,:)=-3*d;
A(i,i+1,:,:)=d;
A(i,n+i-1,:,:)=k(i);
A(i,n+i,:,:)=-(2*k(i)+k(i+1));
A(i,n+i+1,:,:)=k(i+1);
end
A(n,:,:,:)=[zeros(1,n-2) d -2*d zeros(1,n-2) k(n) -2*k(n)];
Any hint or answer would be hugely helpful. Thank you. If you are unclear about the question, pls ask me!

채택된 답변

Steven Lord
Steven Lord 2020년 1월 15일
Build your size vector first. Ideally you have an upper bound on the number of dimensions:
upperBoundOnNdims = 10;
sizeVec = ones(1, upperBoundOnNdims);
Fill in the elements of your size vector however you want.
sizeVec(2) = 3;
sizeVec(3) = input('Enter the size in dimension 3:');
sizeVec(5) = randi([2 10]);
Once your size vector contains the correct values, make an array of the size specified by your size vector. I'm going to make this one with all elements equal to 0, but you could use other functions (ones, rand, etc.) to create it.
A = zeros(sizeVec);
Note that all the elements of sizeVec after the fifth are 1 and that A has five dimensions. Arrays in MATLAB usually have a very large number of trailing singleton dimensions that can be queried but are not displayed.
ndims(A) % 5
size(A, 42) % 1
size(A, 1e6) % 1
  댓글 수: 3
Stephen23
Stephen23 2020년 1월 15일
편집: Stephen23 2020년 1월 15일
"So how can I make this automatic? I would like to avoid adding extra colons..."
You can do that very simply using this syntax:
A(1,1,:) = 1;
This works because the last explicit index does not refer to that specific dimension, but in fact refers to all trailing dimensions, collapsed into one. See the blog discussion here:
Sandeep Parameshwara
Sandeep Parameshwara 2020년 1월 15일
Thanks Stephen for the help, appreciate it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Meg Noah
Meg Noah 2020년 1월 14일
편집: Meg Noah 2020년 1월 14일
If you know the size you want:
A = zeros(nrow,ncol);
or
A = zeros(nrow, ncol1, ncol2,ncol3);
etc.
Can also initialize to all ones.
A = ones(nrow,ncol);
If you want all the initial values to be 5, then
A = 5*ones(nrow,ncol);
If you want all the intial values to be undefined:
A = nan(nrow,ncol);
And there is a function
padarray
that can be used to expand arrays that have already been partially populated.
You can also use vertcat, horzcat, and simply:
A = [A ones(nmorrows,1)]
and so on
  댓글 수: 2
Sandeep Parameshwara
Sandeep Parameshwara 2020년 1월 14일
편집: Sandeep Parameshwara 2020년 1월 14일
Thank you,i know this. But here I want to code in such a way that the code itself should determine the size. I don't want to write A=zeros(5,5,10,10) because next moment i might need one more dimension like A=zeros(5,5,10,10,10) ,in which case the remaining code also changes completely. I want to avoid rewriting everything manually
Meg Noah
Meg Noah 2020년 1월 15일
padarray can be used to extend your array sizes
or you can append or prepend arrays using, for example,
A = [A zeros(1,ncol)]
maybe if you posted your code, and not just a snippet, it woudld be easier to figure out what you're missing.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by