1

Here is what I have:

coluna = zeros(2,3);
coluna(1) = func(3,2,1);
coluna(2) = func(3,4,5);

Here is example of the function:

function vec = func(a,b,c)
    vec = zeros(3,1);
    vec(1,1) = a*b*c;
    vec(2,1) = a+b+c;
    vec(3,1) = a-b-c;
end

This is only an example code, but it illustrate pretty much everything I mean by the problem.

In C++ lets say it would be like this:

int *func(int a,int b,int c){
    int vet[2];
    vet[0]=a*b*c;
    vet[1]=a+b+c;
    vet[2]=a-b-c;
    return vet;
}

int main(){
    int mat[1][2];
    mat[0]=func(3,2,1);
    mat[1]=func(3,4,5);
}

In MatLab it will give me this error:

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

How may I fix this?

1 Answer 1

1

The reason this occurs is because you are doing this:

coluna(1) = ...

When you want to assign a row of an array you need to do this:

coluna(1, :) = ...

similarly for a column:

 coluna(:, 1) = ...

Then you need to make sure the return from func is the same size as the thing you are replacing.

So in your example, this is the correct way to assign the output from func:

coluna = zeros(2,3);
coluna(1, :) = func(3,2,1);
coluna(2, :) = func(3,4,5);
Sign up to request clarification or add additional context in comments.

Comments

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.