What is the difference between these two commands?

A = zeros(9,1)
A = 9×1
0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
s = size(A)
s = 1×2
9 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[m n] = size(A) %This runs without any error
m = 9
n = 1
[m n] = s %But this raises an error
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.

5 Comments

Your final line does not run without error
[m n] = [3 4]
Too many output arguments.
"What is the difference between these two commands?"
The difference is that you define the array on another line. Other than that, they are much the same.
"This runs without any error"
No, both lines will throw errors.
You appear to be attempting to write Python code. However, MATLAB is not Python:
I have edited my question, I am sorry for not checking well before posting the initial question. The modified one is my actual query.
"I have edited my question, I am sorry for not checking well before posting the initial question. The modified one is my actual query."
Your revised code does two totally different things:
  • a function may return multiple output arguments (i.e. multiple arrays), exactly as the documentation explains.
  • arrays are not functions and do not return output arguments.
That is the gist of it.

Sign in to comment.

 Accepted Answer

s = size(A)
That is a function call with one output. The size() function is defined as first checking the number of outputs, and if there are exactly one outputs then size() is to return a vector of size information (with a minimum of two elements in the vector)
[m n] = size(A) %This runs without any error
That is a function call with two outputs. The size() function is defined as first checking the number of outputs, and if there are more than one outputs then all except the last of them are defined to be the size of the corresponding dimension. In the case of multiple outputs, the value of the last output is defined to be the product of all remaining dimensions of the size data.
So for [m n] = size() of something that is 9 x 1, the first output ( m ) is defined as receiving the first element of the size data, which is 9. Then the second output ( n ) is defined as receiving the product of the remaining sizes; the remaining sizes are all trailing 1's, so their product is 1, so n is assigned 1.
[m n] = s
This is not a function call. There is only one output available, namely the content of s . The content of s is matched to the output m and then MATLAB goes searching for a second output to match to n and does not find the second output, so it gives an error message.
You have made the mistake of thinking that size() is always returning a vector, and that the vector is being split when you do [m n] = s. That is not the case: size() is specifically testing the number of outputs and returning different things in different outputs.

More Answers (1)

s = [3 4]
s = 1×2
3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
What you wrote won't work. It may seem deterministic in this scenario but in general it's ambiguous.
try
[m n] = s
catch ME
fprintf("This code threw error:\n%s", ME.message)
end
This code threw error: Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
Why would this syntax be ambiguous in general? Suppose that s had been [3 4 5]. What would you have expected m and n to be in that scenario? Should m be [3 4 5] and n be empty? Should m be [3 4] and n 5? Should m be 3 and n [4 5]? Any of those are reasonable possibilities, and so how would we determine what you wanted in that scenario.
If you wanted to do this type of assignment you could, if you convert s into a cell array.
s2 = mat2cell(s, 1, [1 1])
s2 = 1x2 cell array
{[3]} {[4]}
[m2, n2] = s2{:}
m2 = 3
n2 = 4
This avoids the ambiguity because s2 would have to have the same number of elements as you're assigning into. And when you create the cells, you control exactly what gets stored in each cell. So in that three input case, I could explicitly split it 2 and 1:
s3 = mat2cell(3:5, 1, [2 1])
s3 = 1x2 cell array
{[3 4]} {[5]}
[m3, n2] = s3{:}
m3 = 1×2
3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
n2 = 5

3 Comments

s3 = {1,2};
s3{:} is a comma separated list, isn't it?
s3{:}
ans = 1
ans = 2
which is the same (?) as this comma separated list
1,2
ans = 1
ans = 2
This works
[m,n] = s3{:} % case A
m = 1
n = 2
But this doesn't
[m,n] = 1,2 % case B
Too many output arguments.
Why aren't Case A and Case B the same? Don't both lines have the same comma separated list on the right hand side?
Your case B is different. The comma becomes a satement separator there. For example, we can do this:
a = 1,2
a = 1
ans = 2
The parser breaks what you think of as the right hand side into two distinct statements.
You might counter with this:
C = cell(1,2);
C{:} = 1,2
Assigning to 2 elements using a simple assignment statement is not supported. Consider using comma-separated list assignment.
As you can see, the parser is not recognizing 1,2 as a comma separated list.
Internally generated comma-separated lists are "virtual", but hard-coded commas are syntatical.
The situation is similar to
a = [9 5]
a = 1×2
9 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
var = []
var = []
try
a(2) = var
catch ME
warning(ME.message)
end
Warning: Unable to perform assignment because the left and right sides have a different number of elements.
try
a(2) = []
catch ME
warning(ME.message)
end
a = 9
The hard-coded [] is recognized syntactically as deletion, but the variable with value [] is not recognized as deletion

Sign in to comment.

Categories

Products

Release

R2024b

Asked:

on 12 Dec 2024

Commented:

on 19 Dec 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!