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);endA(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!
Best Answer