MATLAB: 3D matrix (I guess is an indexing question?)

MATLABmatrix array

I have a 9x9x9 matrix (simple). It is created by:
matr=zeros(9,9,9);
for ct=1:9
matr(:,:,ct)=ct;
end
I then type: (step 2, see below)
for ct1=1:9
for ct2=1:9
k=gen;
matr(ct1,ct2,:)=[matr(ct1,ct2,[1:(k-1) (k+1):9]) 0];
end
end
where gen is a function that generates a random integer from 1 to 9, inclusive.
I want my code to be able to:
Step 1: For every 9×9 position, a value of unique k is generated.
Step 2: The value of k is removed in that position in the 3rd dimmention vector. Every numbers after k are shifted forward, and a zero is added on the last element. (e.g. initial matr(2,1,3)=1:9, then matr(2,1,3)=[1:6 8 9 0] if ct1=2, cy2=1 and k=7)
Step 3: The ultimate plan is to run this bloc of code multiple times (less than 6 times, and the value of k will not be repeated).
An example to illistrate what do I mean in step 3:
Let's say ct1=4, ct2=6, k=5. First run results from matr(4,6,3)=1:9 to matr(4,6,3)=[1:4 6:9 0]. This blog of code runs again and k is now 2. So it will give matr(4,6,3)=[1 3:4 6:9 0 0], and so on. However, my code is not able to do this at the moment, I will consider this once step 2 (this problem) is out of the way.
It returns an error (In red: Dimensions of arrays being concatenated are not consistent.), even I changed the line to:
matr(ct1,ct2,:)=[matr(ct1,ct2,[1:(k-1) (k+1):9]);0];
Please help.

Best Answer

matr(ct1,ct2,:)=[1:(k-1) (k+1):9 0];
Is this what you want?