can you tell me more about 'repmat'?

2 views (last 30 days)
more than matlab help.
like
what it does?
why it required?
how it works?
and other aspects you know.
and also any codeor code module in general matlab we can use in place of "repmat".
plz explain in detail.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Aug 2013
You can look at the source code for repmat. It is roughly:
let N be the size() of the array along the dimension in question. Construct M = 1:N (vector of indices). Now if the array is to be repeated P times, construct a vector Q which is P copies of M appended to each other. Once you have that, index the original array at Q
for example, if the array is of length 2 repeated 3 times, then you would construct Q = [1 2 1 2 1 2], and then index array(Q,:) which is array([1 2 1 2 1 2], :)
There is no magic to it, just brute force indexing to repeat an array a number of times. There are a lot of different reasons one might want to do that.
  3 Comments
Walter Roberson
Walter Roberson on 13 Aug 2013
That is like asking what the reasons are that one might want to multiply.
I will give an example of a less common use:
Suppose you need to construct a format string for use with textscan(). The number of elements that will be on one line is, in this example, not known ahead of time, but is known at the time one wants to construct the format string. Each format element can (in this example) be written as '%d'. Suppose the number of entries on the line is given by the variable N. Now how do you construct the string '%d%d%d%d...%d' where there are N copies of the '%d' ?
You could use a loop to construct the string. You could get fancy and do binary decomposition of N in order to find the way to construct the string using the fewest concatenation operations. Or you can go for simplicity and use repmat(): repmat('%d', 1, N)
There are many many other uses for repmat.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!