How can I make a vector of constant value which length is 100 not using 'for&while'.
Show older comments
How can I make a vector of constant value which length is 100 not using 'for&while'.
for example I want to make 'a'. a=[3 3 3 3 3 ........ 3 3 3 3]
Accepted Answer
More Answers (1)
Andrew Potvin
on 1 Aug 2023
Edited: Andrew Potvin
on 1 Aug 2023
1 vote
The above is NOT the approved MATLAB approach. It requires relatively slow floating point math (or repmat). Instead, do this:
a(1,1:100) = 3;
3 Comments
That works, but if speed is of concern, it's not as fast as it might seem. At least as far as my testing goes, it's consistently the slowest of the given examples in cases where I assume a person might care about the cost. I've tested this in R2009b, R2015b, and R2019b on my machine, as well as here on the forum. Expansion by indexing can be faster in old versions like R2009b when the output size is small (100 elements). Consider though that a 50% speed advantage doesn't mean much when the times are ~10-20us. It doesn't pay much to reduce the cost of things only in cases where they're cheap to begin with.
k = 3;
sz = [1 1E6];
timeit(@() testA(k,sz))
timeit(@() testB(k,sz))
timeit(@() testC(k,sz))
function testA(k,sz)
A = k + zeros(sz);
end
function testB(k,sz)
B = repmat(k,sz);
end
function testC(k,sz)
C = k(ones(sz));
end
Bear in mind that repmat() has had some significant improvements to speed in the last 14 years (e.g. R2013b). A lot of books discourage the use of repmat(), though that should be kept in context. Also consider that many of the particular reccomendations against repmat() aren't necessarily questionable because repmat() improved, but because the alternatives they suggest have since been superceded (e.g. bsxfun()).
At times , expansion by indexing can be more succinct and with comparable cost.
A = repmat(mypict(:,:,2),[1 1 3]); % perfectly sensible
B = mypict(:,:,[2 2 2]); % compact, readable, and just as fast
Ultimately, there are plenty of cases where the relative advantages are small enough that I think it's entirely understandable that compactness, clarity, or personal preference might take precedence over performance either way.
Andrew Potvin
on 2 Aug 2023
I'm curious what happens when you also try the following in your various MATLAB's:
function testD(k,sz)
D(1:sz(1),1:sz(2)) = k;
end
I'm guessing you'll find it's faster. If you really want to overkill this little experiment, perhaps log the total time of running each test 1000 times.
My original point was simply that
M(1:Nrows,1:Ncols) = c;
is the MATLAB-iest way to initialize a vector/matrix with a single constant value.
;-)
- AP
This is what I get:
% in R2023a (forum):
% 0.49ms 0.27ms 3.8ms 0.93ms
% in R2019b:
% 2.1ms 0.34ms 12ms 5.1ms
% in R2015b:
% 2.1ms 0.55ms 17ms 8.2ms
% in R2009b:
% 4.6ms 3.4ms 12ms 9.8ms
Logging the total times would require modifying timeit() in a manner that defeats its existing functionality. Timeit() already does multiple calls and a bunch of work to eliminate the influence of cycle variation and overhead.
My hardware is a 13-year old 6-core AMD board that I got out of a dumpster. Take that with a grain of salt.
Mind you, none of these are really all that bad. The difference between 0.5ms and 5ms is probably negligible; I've just seen too many books full of old recommendations that I know need a big asterisk next to them.
Categories
Find more on Matrix Indexing 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!