What is the difference between these two commands?
4 views (last 30 days)
Show older comments
A = zeros(9,1)
s = size(A)
[m n] = size(A) %This runs without any error
[m n] = s %But this raises an error
5 Comments
Stephen23
on 12 Dec 2024
"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.
Accepted Answer
Walter Roberson
on 12 Dec 2024
Edited: Walter Roberson
on 12 Dec 2024
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.
0 Comments
More Answers (1)
Steven Lord
on 12 Dec 2024
s = [3 4]
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
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])
[m2, n2] = s2{:}
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])
[m3, n2] = s3{:}
3 Comments
John D'Errico
on 12 Dec 2024
Edited: John D'Errico
on 12 Dec 2024
Your case B is different. The comma becomes a satement separator there. For example, we can do this:
a = 1,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
As you can see, the parser is not recognizing 1,2 as a comma separated list.
Walter Roberson
on 12 Dec 2024
Internally generated comma-separated lists are "virtual", but hard-coded commas are syntatical.
The situation is similar to
a = [9 5]
var = []
try
a(2) = var
catch ME
warning(ME.message)
end
try
a(2) = []
catch ME
warning(ME.message)
end
The hard-coded [] is recognized syntactically as deletion, but the variable with value [] is not recognized as deletion
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!