1

I have a problem with defining some matrices in MATLAB. I get three numbers x,y,z as an input from user, then I want to create y-1 empty matrices. For example consider x = 3, y = 4, and z = 2. The required y-1 matrices M1, M2, and M3 are:

size(M1) = [3,4] ~ [x,y]

size(M2) = [4,4] ~ [y,y]

size(M3) = [4,2] ~ [y,z]

The parameters x,y are not known before running the program. If y was 5, the matrices were:

size(M1) = [3,5] ~ [x,y]

size(M2) = [5,5] ~ [y,y]

size(M3) = [5,5] ~ [y,y]

size(M4) = [5,2] ~ [y,z]

Indeed the main problem is that the number of matrices is an input. Please guide me on how I can create a function loop to define this matrices.

4
  • 1
    Empty in MATLAB would be A=[], a zero matrix (e.g. `A = zeros(3)') still has some size. Commented Aug 29, 2015 at 14:22
  • @Jubobs, zero or one, or any other initial value is not important since the values will revised in the algorithm. Problem is that the number of matrices is an input. Commented Aug 29, 2015 at 14:31
  • You'll want to do this in a cell-class for sure. bsxfun and cellfun however cannot handle zeros. I'll keep digging. Commented Aug 29, 2015 at 14:40
  • If the number of variable is an input, you have to use a cell array, which will still have the benefit of matrix style indexing, or a structure, which is convenient when you need (or prefer) to name your variables. Commented Aug 29, 2015 at 14:41

3 Answers 3

4
X = input('Enter X please: '); 
Y = input('Enter Y please: '); 
Z = input('Enter Z please: '); 
Cells={}
Cells{1}=zeros(X,Y);
for i=2:Y-1
 Cells{i}=zeros(Y,Y);   
end;
Cells{Y-1}=zeros(Y,Z);
Sign up to request clarification or add additional context in comments.

5 Comments

I was just about to type this up. Kudoos
Probably the solution. Depending on the logic it might be simpler to use Cells{end+1}=.. so there is no need for bookkeeping of indices.
How I can call the value of cell [i,j] in one of the matrices?
you can use of this term "Cells{prob}(i,j)" or use of new matric same as tmp=Cells{prob};
Thanks a lot for the explanation .
4

You could do this without using cells, but I strongly advice you not to, so: One way to do this, with each matrix being part of a cell:

dims = str2num(input('Type in selected x,y,z: ', 's'));

M = arrayfun(@(n) zeros(dims(n), dims(2)), [1 2*ones(1,y-1) 3], 'UniformOutput', 0)

%% In the command window:
Type in selected x,y,z: 3 4 2

M = 
    [3x4 double]    [4x4 double]    [2x4 double]

Note that with the str2num(input()) approach, you can input both: [4 3 2], [4, 3, 2], 4 3 2, 4, 3, 2 or even 4;3;2. It's basically impossible to make mistakes here!

The way this works is: arrayfun performs an operation for each elements of the vector [1 2*ones(1,y-1) 3]. The operation is to create a matrix of zeros, with the desired dimensions. UniformOutput is a parameter that must be set to false, or 0 if the output is something other than scalars.

To access, and make changes to any of the matrices:

When you type M{x}, you can think of that as the equivalent of just a matrix name, i.e. it's fine to use () to index the matrix, straight after the {}.

So, you can do:

M{1}(3,3) = 2;

which would assign the value 2 to the element (3,3) in matrix 1.

Comments

0
M1 = zeros(x,y);
M2 = zeros(y,y);
M3 = zeors(z,y);

Simple enough. Though why M2 and M3 in your question are the same I haven't figured out yet.

3 Comments

The number of matrices is an input !,
So why are M2 and M3 exactly the same? You want M1 and M(end) to be (x,y) and (z,y) and everything else as (y,y)? I still do not understand that part.
Yes, the the first and last matrices are (x,y) and (z,y) and the remainder are (y,y). The problem is that there are "y-3" matrices so that I cannot simply write zeros(y,y). The number of those matrices is itself an input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.