can you tell me more about 'repmat'?

2 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2013-8-11
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 个评论
Walter Roberson
Walter Roberson 2013-8-13
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by