[ ] or ( ) in constructing array

In constructing array, we can have different styles as show below. Personally, I found that using [ ] is a better choice in MATLAB since it reminds that an array is created. Using ( ) tends to give an impression that an expression or function arguments are going to used when you quickly browse through the code.
MATLAB code analyser in the editor gives a warning message when using statement such as b=[1:2:300000] indicating that [ ] are not necessary.
I am wondering if there is a performance penalty using [ ] for constructing array or any disadvantages for discouraging the use of [ ]. It is also a bit annoying the code analyser give an warning on this if there is nothing wrong to use [ ].
% Style 1 using ( )
tic
for i=1:1e3
a=(1:2:300000);
end
toc
Elapsed time is 0.179663 seconds.
% Style 2 using [ ]
tic
for i=1:1e3
b=[1:2:300000];
end
toc
Elapsed time is 0.176980 seconds.
% Style 3
tic
for i=1:1e3
a=1:2:300000;
end
toc
Elapsed time is 0.184836 seconds.

7 Comments

It is unethical to delete the thread at this point of time.
Disappointed. Very Disappointed.
I try to assume good faith, especially for frequent contributors. Do you still think it is unethical to delete an answer if it is an attempt at de-escalation?
I will have a look at this thread later to see if I can voice my thoughts in a new answer without fanning the flames.
@Rik
First, it has nothing to do with how frequent you contribute.
I also delete answers that I am not satisfied. But do you think this is the same case?
Bruno Luong
Bruno Luong on 19 Jul 2022
Edited: Bruno Luong on 19 Jul 2022
@Chunru, in the queston in this thread one can read the (error) message from TMW
"...To construct matrices, use brackets instead of parentheses..."
Somehow I were thinking of you when I read it. :-)
@Bruno Luong Thanks for the info. To me, it is a bit of style and a bit of habit (good or bad) to use square brackets sometimes. I actually lost the track where I got the habit :-(. There was no code analyser for very long time since I started to use MATLAB. Many "bad" habits may be picked up along the way.
Perhaps I should also blame MATLAB for that trait of possibly bad habit (just kidding, I am not blaming MATLAB for this :-))

Sign in to comment.

Answers (4)

Bruno Luong
Bruno Luong on 16 Jul 2022
Edited: Bruno Luong on 16 Jul 2022
The only danger is that unexpected results when someone is careless about indentation and spacing
a = 1 : 5 +2
a = 1×7
1 2 3 4 5 6 7
b = [1 : 5 +2]
b = 1×6
1 2 3 4 5 2
c = (1 : 5 +2)
c = 1×7
1 2 3 4 5 6 7

1 Comment

Good points.
However, I think the reason is still not strong enough for discouraging use of [ ] when the style is concerned.

Sign in to comment.

The fastest and most bug-free piece of code you can write is the one that doesn't exist because it doesn't need to exist.
When I see someone write a piece of code like:
x = [1:10]
I get a little tingling in my nose, as this is a (very mild but still detectable) code smell. Did the author of that code intend to concatenate something together with that 1:10, and so this line has a bug? What are they trying to convey with those square brackets above and beyond what's conveyed by that colon operator? You said "since it reminds that an array is created." but IMO that's not what reminds the reader of the code that an array is created. The : does.

13 Comments

Bruno Luong
Bruno Luong on 16 Jul 2022
Edited: Bruno Luong on 16 Jul 2022
but IMO that's not what reminds the reader of the code that an array is created. The : does.
The "IMO" is important.
The ":" can create a very specific type of array: one row with equidistance between two adjacent elements, with "[]" one can create any type of array in theory.
And now you know that your view is not universally shared Steve.
Oh, I already knew that. Perhaps I should have said that in this context it's not the [] that indicates array creation, it's the colon. In other contexts commas or spaces (to separate elements in a row) or semicolons or line breaks (to separate rows) are indicators of array creation. In others it's the presence of the cat function.
x = cat(3, 1, 2, 3)
x =
x(:,:,1) = 1 x(:,:,2) = 2 x(:,:,3) = 3
Stephen23
Stephen23 on 16 Jul 2022
Edited: Stephen23 on 16 Jul 2022
"with "[]" one can create any type of array in theory. "
No, square brackets cannot concatenate along the third or higher dimensions.
"And now you know that your view is not universally shared Steve."
And yet ironically you highlight the exact problem with the OPs "style": it is precisely because square brackets are used to concatenate horizontally and/or vertically (but not along higher dimensions) that using them to concatenate one array with nothing else is misleading: the OP is not "creating any kind of array" using the square brackets, for the simple fact that the array already exists.
You still missing the point, some people prefer to *highlight* an array with []; The only purpose is the subjective "style" that some people might prefer. We don't use it because we expect it to do something superfluous in the computation (or because we are dumb in MATLAB to your view), we use it to help us to read the code.
A = [ [0], [1,2,4];
[1;
2;
4], [zeros(3)] ]
The above might be completely awful to your highe-level MATLAB expertise, to me I see better the block structures of my matrix, sometime closer to a notation of some book or paper.
Fair enough. Code Analyzer doesn't block you from doing that, it simply says there may be an issue or an inefficiency here.
But if you are really annoyed with Code Analyzer for doing that, you can suppress that particular message. The "Adjust Code Analyzer Message Indicators and Messages" section on this documentation page states how you can suppress a message on that particular line, in the whole file, or in all files.
@ Bruno Luong about 3 hours ago
You still missing the point, some people prefer to *highlight* an array with []; The only purpose is the subjective "style" that some people might prefer. We don't use it because we expect it to do something superfluous in the computation (or because we are dumb in MATLAB to your view), we use it to help us to read the code.
==> Exactly that is what I intend to do using [ ]. So I am one of the some people :-). One can say it is not necessary. But if there is no harm, why not use it for your benefit?
Stephen23
Stephen23 on 17 Jul 2022
Edited: Stephen23 on 17 Jul 2022
"You still missing the point, some people prefer to *highlight* an array with [];"
I am perfectly aware that the OP wants to "highlight" an array, however the the OP's question title is not "[ ] or ( ) in highlighting an array", but is in fact "[ ] or ( ) in constructing array", and then it procedes to repeat this incorrect staement, e.g. " ...is a performance penalty using [ ] for constructing array" when in fact none of their examples or given use-cases related to this question actually use square brackets for constructing arrays. That is the sole point that I am making.
Can someone please show me how I can use "( ) in constructing array" , as the title states?
" But if there is no harm..."
There clearly is a harm: a large number of other users who find that style misleading.
"...why not use it for your benefit"
You are not just using that misleading "style" for "your benefit", but also on this forum.
"to me I see better the block structures of my matrix, sometime closer to a notation of some book or paper."
Of course, if in a specific situation it makes code clearer, then by all means use it (just like any MLINT message, which can be ignored when the situations requires it). But the original question is not asking about specific "notation of some book or paper", it asked for a general advice on a general coding style, to be applied every single time the author creates an array using the colon operator.
Including superfluous code as a general rule is not suported by your comment (in specific situations -> decide to ignore MLINT).
Force-selling are uncalled for to say at least. Keep policing around is not welcome. Harsh words and aggressive activities are not too far away from harassment, bullying and gastership. Nobody against you to practises whatsoever you preach, as long as you are not damaging the forum.
Do you think there is no damage done to the forum for such activities?
If there is a need, we should stand up to make the forum better.
"You are not just using that misleading "style" for "your benefit", but also on this forum"
Can I say that you damaging this forum by force selling?
It feels like this thread is getting a bit heated. How about we all take a little break from it, say 24 hours, and then if there's still stuff to be said on this topic come back a little calmer? Thanks.
No problems.
Chunru
Chunru on 17 Jul 2022
Edited: Chunru on 17 Jul 2022
Since some threads had been deleted, I don't think I need further restrain myself from posting.
I think all of us need a mirror and dictionary for reflection. If someone is lacking of these equiments, it is time to equip them. Try to look up dictionaries and find out what are respect, courtesy, mildness, ethics are about. If there is time to spare, also try out some words like arrogance, accusation, speculation and so on.
Try to stand in front of a mirror for some seconds. Hopefully we can find somthing.
This is a place for everyone to enjoy learning. It is not a place for someone to monopoly, bully and harass. Wish all of us have peaceful place.
@Chunru your posts still come across to me as very aggressive. To such a degree in fact that I don't feel my opinion would be treated as opinion, but instead as a personal attack.
Please be aware that there may be linguistic and cultural barriers. I have not seen attempts to monopolize the discussion (most users here have sufficient moderation rights to completely edit away any dissent). As to the bullying and harassment; it is difficult to judge the intent from text alone. Given the long history of most users in this thread, I am inclined to think they do not intend their posts to be interpreted like that. That is what I meant by 'frequent contributor'.
I appreciate your effort on mitigating the situation. I have no intention to escalate the things and I just want to have a peace of mind. I am not sure if you have followed through the conversations and I am ready to appologize if I have been on the wrong side.
"Given the long history of most users in this thread, I am inclined to think they do not intend their posts to be interpreted like that. That is what I meant by 'frequent contributor'."
Talking about frequent contributors, I am amazed with many of them and adimired them as heros. I may be wrong to say "it has nothing to do with how frequently one contribute". A second thought is that the more one contributes, the bigger responsibilities one should have. Perhaps there should be an even higher standard to be expected.
I thank you again for your effort. All I am trying to advocate is nice place for each of us.
Many thanks.

Sign in to comment.

just use
b = 1:2:300000;
When you use [] then the call would be equivalent to
b = horzcat(1:2:300000);
which contains an unnecessary function call and can potentially remove an opportunity for optimization

1 Comment

Really?
foo
Warning: MATLAB debugger can only stop in MATLAB code files, and "libmwm_interpreter>horzcat" is not a MATLAB code file.
Instead, the debugger will stop at the point right before "libmwm_interpreter>horzcat" is called.
ans = 1×10
4.1416 5.1416 6.1416 7.1416 8.1416 9.1416 10.1416 11.1416 12.1416 13.1416
function y = foo
dbstop in horzcat
x = pi;
a = [1:10];
y = a + x;
dbclear all
end
A don't have any stop triggered when I run foo. with the "superflous" [] The code analyzer and engine are smart enough so horzcat is not explicitly called. It is really stupid if the mlint is more intelligent than the engine.

Sign in to comment.

I am wondering if there is a performance penalty using [ ] for constructing array
If you measure inside a function, and if you discard the first 10-ish measurements (which typically have peak times that are substantially higher, for reasons I do not fully understand), then the time using [] is sometimes as low as the time without [], but typically peaks 6 to 8 percent higher.
N = 50;
t1 = zeros(N,1);
t2 = zeros(N,1);
for K = 1 : N
S = tic; without(); ; E = toc(S); t1(K) = E;
S = tic; with(); E = toc(S); t2(K) = E;
end
t1(1:10) = []; t2(1:10) = [];
plot([t1,t2]); legend({'no []', '[]'});
mean(t1), std(t1)
ans = 1.7918e-04
ans = 1.5834e-06
mean(t2), std(t2)
ans = 1.8140e-04
ans = 5.5645e-06
function without()
data = 1:2:300000;
end
function with()
data = [1:2:300000];
end

10 Comments

Increase N to 500, this is the result on my computer R2022a. It looks to me there is hardly any difference.
I have noticed distinct timing pattern differences between Answers and my desktop. A possibility is that timing patterns might hypothetically be different for Linux than for Mac
Is Matlab obligated to actually execute the assignment to data in with() and without()? data is not used, and the intepreter could detect that there aren't any side effects from evaluating the right hand side. From what I can tell, it actually does perform the assignment.
Bruno Luong
Bruno Luong on 17 Jul 2022
Edited: Bruno Luong on 17 Jul 2022
@Paul I mofify Walter's code and returns data to main script. The tic/toc result is sensible identical.
Is Matlab obligated to actually execute the assignment to data in with() and without()?
It is if you return the value. The only documented optimization between calling levels has to do with a case where data can be updated "in place" instead of using copy-on-write.
There are reasons why you might want to waste time, so optimization should not remove a chain that does not affect output... but there are cases where code might not be executed exactly as written, if there is a code pattern that is implemented by one of the high speed libraries.
N = 100;
t1 = zeros(N,1);
t2 = zeros(N,1);
for K = 1 : N
S = tic; without(); ; E = toc(S); t1(K) = E;
S = tic; with(); E = toc(S); t2(K) = E;
end
t1(1:10) = []; t2(1:10) = [];
plot([t1,t2]); legend({'no []', '[]'});
mean(t1), std(t1)
ans = 1.8352e-04
ans = 6.5381e-06
mean(t2), std(t2)
ans = 1.8341e-04
ans = 5.8787e-06
function data = without()
data = 1:2:300000;
end
function data = with()
data = [1:2:300000];
end
@Bruno Luong, I had did the opposite, i.e. commented the assignment from with() so that that with() is basically a no-op, and saw a timing change, which is how I concluded the assignment, though unneccessary, was actually happening.
@Walter Roberson, For me, I might want Matlab to optimize away that assignment every time. If I want code to waste time (I never do but realize that others might) I'm sure there must be better ways, like pause() or something. OTOH, I'm not sure how such optimization would interact with the debugger and profiler, so maybe (probably) there's much more to that kind of optimization than meets my eye.
Interestingly, at least to me, Matlab creates the array even with this line at the command prompt
tic, for ii = 1:5000,1:2:300000;end,toc
Elapsed time is 0.874310 seconds.
tic, for ii = 1:5000, end,toc
Elapsed time is 0.000226 seconds.
Bruno Luong
Bruno Luong on 17 Jul 2022
Edited: Bruno Luong on 17 Jul 2022
May be because the reason is that when you do an expression without assign to a variable, matlab still must assign to ANS, so all expression without user assigment must be still evaluated.
Paul
Paul on 17 Jul 2022
Edited: Paul on 17 Jul 2022
Great observation. Maybe it could be evaluated once instead of 5000 times.
There are some cases where an assignment of a literal colon expression will copy the results into a hidden variable, and then on later lines that use the exact same literal colon expression including spacing before any comment, will use the shadow variable. The boundary is on the order of 4 kilobytes if I recall correctly.
Unfortunately I have had difficulty locating the relevant discussion. I seem to recall that James Tursa raised the issue, having noticed that altering an element of a colon expression within a mex routine also affected a different colon expression.
"Busy work" to delay cannot be completely replaced by pause()
  • the resolution of pause is 1/100 s or 1/1000 s (Mac) which is often not good enough
  • pause() is handled by operating system calls, which give up control of the core. When using hyperthreads the other thread gets control over the core until it volunteers to give up control. When not using hyperthreads, giving up the core means that when the timer expires in the kernel, your process gets scheduled for execution, with an indefinite delay before execution depending on operating system policy and competition for resources
  • generally speaking, delays in the kernel show up quite differently for side-channel and differential analysis for the purposes of covert attacks. On the other hand, I am not convinced that MATLAB is suitable for writing software intended to resist those attacks.

Sign in to comment.

Categories

Asked:

on 16 Jul 2022

Commented:

on 20 Jul 2022

Community Treasure Hunt

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

Start Hunting!