0
votes

I'm trying to write a Matlab code that, given a matrix, outputs 3 matrices (according to some rules). I'm having difficulty getting this to work though - I can't output a vector with matrices as entries. I get the error message:

??? In an assignment A(I) = B, the number of elements in B and I must be the same.

How can I go about doing this?

3
please show us (the relevant portion of) the code you've already got - BioGeek

3 Answers

4
votes

You could write

function [A B C] = myFunction(X)
    A = X;
    B = 2 * X;
    C = 3 * X;
end

and call it with

[a b c] = myFunction(ones(2))

If you won't want all of the outputs, just call it with

a = myFunction(ones(2))

or

[a b] = myFunction(ones(2))

to get just the first argument, or just the first two arguments.

1
votes

You can also use cells:

A=cell(1,3); %% or A=cell(1,2); if you want to output only 2 matrices
A{1}=B;
A{2}=C;
A{3}=D;

If your matrices all have the same size you can also concatenate them:

A=zeros(m,n,3);
A(:,:,1)=B;
A(:,:,2)=C;
A(:,:,3)=D;
0
votes

Function declaration:

 function [A, B, C] = something (Input_mat)
 %Do whatever needs to be done here, for example:
 A= Input_mat;
 B= Input_mat';
 C= ones(18);

And then when you call it using:

 [A,B,C] = something (Some_mat)

A, B and C are filled.