assigning values with []

5 次查看(过去 30 天)
[a,b]=[1,2]
Why doesn't this work? How to rewrite into what is easier and more succinct than a=1; b=2?
Just to respond to all the comments and answers, there's certain occasions where my suggested (wrong) syntax is easier than others, e.g. when parameters are defined in an array in 1 go.
  2 个评论
Image Analyst
Image Analyst 2025-4-13
编辑:Image Analyst 2025-4-13
More succint? 11 characters is more succint than 8 characters?
length('[a,b]=[1,2]')
ans = 11
length('a=1;b=2;')
ans = 8
So it's not shorter and easier I think is a matter of opinion.
Also I think your suggestion could be ambiguous. Does [a,b]=[1,2] mean a=1;b=2, OR might someone think it could possibly mean that both a AND b are equal to the vector [1,2], like a=[1,2];b=[1,2]. So I think the a=1;b=2 method is a lot more intuitive - there is no possibility of misunderstanding what that means. It's unambiguous and more succint.
feynman feynman
feynman feynman 2025-4-14
thanks for your comment which makes a lot of sense

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2025-4-13
编辑:John D'Errico 2025-4-13
The funny thing is, I have at times wanted a syntax like you wrote to work in MATLAB, but it won't. We could probably find some significant issues if you did try to modify the MATLAB language so that programming syntax would work.
But you could surely write code that would do it for you though. Maybe something like this:
function [varargout] = vecsplit(ab)
% vecsplit - splits a vector into distinct elements as output arguments
varargout = mat2cell(ab(:),ones(numel(ab),1),1);
end
[a,b] = vecsplit([1 2])
a = 1
b = 2
It will even work with more than 2 elements.
[a,b,c] = vecsplit([1 2 3])
a = 1
b = 2
c = 3
If you only provide 2 outputs, it returns only the first two elements.
[a,b] = vecsplit([1 2 3])
a = 1
b = 2
And it works on arrays too.
[a,b,c,d] = vecsplit([1 3;2 4])
a = 1
b = 2
c = 3
d = 4
And if you wanted only the first and third elements? Hmm, I think it should work.
[a,~,c] = vecsplit([1 2 3])
a = 1
c = 3
But is that really easier than just the trivially easy to write
a = 1;
b = 2;
Personally, I don't see a big gain, but as you like it. I suppose this allows you to extract scalars from a vector of elements on the fly, as returned from another function. The beauty of a tool like MATLAB is you can extend it in any way you see fit.
  2 个评论
Steven Lord
Steven Lord 2025-4-13
编辑:Steven Lord 2025-4-13
We could probably find some significant issues if you did try to modify the MATLAB language so that programming syntax would work.
Off the top of my head, what would you expect this to do (were it not commented out so I can run code later in this answer)?
% [a,b] = [1,2,3]
There are several different possibilities:
  • MATLAB throws an error
  • a gets the value 1, b gets the value 2, 3 gets ignored
  • a gets the value 1, b gets the values [2 3]
  • a gets the values [1 2], b gets the value 3
Currently, MATLAB throws an error if you try to do this. If you suggest MATLAB should do one of the other three possibilities (or a possibility I haven't listed) what should this do?
% [a,b,c] = [1,2]
I'd also like to point out, if we use the number of characters in the command as the metric we're using for "succinctness", the way you'd do this in MATLAB now is the most succinct. [SL: fixed typo, one too few c's in succinctness]
lengthOfCurrentCommand = strlength("a=1;b=2;")
lengthOfCurrentCommand = 8
lengthOfProposedCommand = strlength("[a,b]=[1,2];")
lengthOfProposedCommand = 12
lengthOfDealVersion = strlength("[a,b]=deal(1,2);")
lengthOfDealVersion = 16
John D'Errico
John D'Errico 2025-4-13
编辑:John D'Errico 2025-4-13
Yes. That was where my head was going, about potential questions. And we cold just arbitrarily choose one of the possibilities Steve suggested. The vecsplit function I wrote makes some arbitrary choices. And that would make some of the people happy, some of the time.
Or ...
we could just stay with the existing syntax, which also makes some of the people happy, some of the time.
Ya can't win.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by