What is the difference between these two commands?
古いコメントを表示
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 件のコメント
Mike Croucher
2024 年 12 月 12 日
編集済み: Mike Croucher
2024 年 12 月 12 日
Your final line does not run without error
[m n] = [3 4]
"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:
Sristi
2024 年 12 月 12 日
Stephen23
2024 年 12 月 12 日
"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.
Sristi
2024 年 12 月 19 日
採用された回答
その他の回答 (1 件)
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 件のコメント
s3 = {1,2};
s3{:} is a comma separated list, isn't it?
s3{:}
which is the same (?) as this comma separated list
1,2
This works
[m,n] = s3{:} % case A
But this doesn't
[m,n] = 1,2 % case B
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?
John D'Errico
2024 年 12 月 12 日
編集済み: John D'Errico
2024 年 12 月 12 日
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.
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
カテゴリ
ヘルプ センター および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!