creating multiple copies of an array

44 views (last 30 days)
ahmed abdelmgeed
ahmed abdelmgeed on 16 Dec 2019
Commented: Guillaume on 17 Dec 2019
I have the array x = [0 1 0 0 1 1 1 0 1 0],
and I would like to create more than one copy of it with different names, for example 5000 copies with names x1, x2, x3, x4, x5 and so on, how can I do that using a loop ?
so it will look like that
x1 = [0 1 0 0 1 1 1 0 1 0]
x2 = [0 1 0 0 1 1 1 0 1 0]
x3 = [0 1 0 0 1 1 1 0 1 0]
x4 = [0 1 0 0 1 1 1 0 1 0]
x5 = [0 1 0 0 1 1 1 0 1 0] and so on
  4 Comments
Stephen23
Stephen23 on 16 Dec 2019
Edited: Stephen23 on 16 Dec 2019
"indexing will be hard because I will need to copy it for more than one time..."
Copy-and-pasting code is a sign that you are doing something wrong.
Using numbered variables is a sign that you are doing something very wrong.
"...and not 5 times most probably I will need to copy it for 5000 times which is hard to do 5000 variables manually."
I have no idea why you think that indexing requires doing anything "manually": the whole point of indexing is to avoid having 5000 separate variables in your workspace (which is an approach that will force to you write slow, complex, buggy code) and make your code simple and efficient.
"...this is why I asked to do it in a loop"
One of the most common uses of indexing is to efficiently access data inside loops.
Like most experienced MATLAB users I often efficiently access data insde loops (yes, even much more than 5000 data arrays) and I never waste my time copy-and-pasting code or writing slow, complex code to fudge my way around many numbered variables. Why do you need to do this?
ahmed abdelmgeed
ahmed abdelmgeed on 16 Dec 2019
Edited: ahmed abdelmgeed on 16 Dec 2019
ok, i got it but if I would like to do how the code will look like ?
I tried this one
x = [0 1 0 0 1 1 1 0 1 0];
sz=5000;
for i=1:sz
A{i}=x;
end
but it gave me that error
Unable to perform assignment because brace indexing is not supported for variables of this
type.
Error in (line 4)
A{i}=x;

Sign in to comment.

Answers (2)

Karthick S
Karthick S on 16 Dec 2019
x = [0 1 0 0 1 1 1 0 1 0];
sz=5000;
for i=1:sz
A{i}=x;
end
  9 Comments
Stephen23
Stephen23 on 16 Dec 2019
"I want to access the variables ..."
because you are trying to force yourself into writing slow, complex, inefficient code.
"clearly to plot C1 from(0 to 9) and when it finishes plot C2 from (10 to 19) and so on. "
Clearly to plot that you just need some simple, efficeint indexing.
"actually... I have a waveform data of (0's and 1's) stored in a csv file ... and the blocks are sent alternatively one after the other in the same figure the end point of a block is the starting point of the next one. thats exactly what I wanto to do."
Sounds perfect for the application of some simple, efficeint indexing. What is stopping you?
ahmed abdelmgeed
ahmed abdelmgeed on 17 Dec 2019
Edited: ahmed abdelmgeed on 17 Dec 2019
please can you help me in plotting this array [0 1 0 0 1 1 1 0 1 0] 50 times in the same figure so that the starting point of a time is the end point of the previous one (plus one), so if the previous one ends at 9 the new one starts at 10. but without using (repmat) I need that each time is a separate array.
1st time of the array [0 1 0 0 1 1 1 0 1 0] is from (0-9)
2nd time of the array is from (10-19)
3rd time of the array is from (20-29), and so on
but each time is a separate array dont want to combine them all on one array like function "repmat"

Sign in to comment.


Guillaume
Guillaume on 16 Dec 2019
"indexing will be hard because I will need to copy it for more [...} I will need to copy it for 5000"
Please read Stephen's link (maybe we should create a FAQ focussed just on numbered variables rather than eval). What Stephen meant by indexing is to use x{i} or x(i, :) or similar, i.e proper matlab indexing rather than numbered variables where the index is embedded in the variable names.
Numbered variables are a very bad idea. Use indexing instead and you don't have to copy anything.
You can either create a cell array, since you want to copy your array multiple times, it's dead easy, no loop needed
x = [0 1 0 0 1 1 1 0 1 0]
numrepeat = 5000;
newx = repmat({x}, numrepeat, 1); %duplicate one-cell cell array numrepeat times
%from now on, whenever you were using xi, use newx{i}
Or you could store the whole lot into a 2D matrix which is more efficient memory wise:
x = [0 1 0 0 1 1 1 0 1 0]
numrepeat = 5000;
newx = repmat(x, numrepeat, 1); %duplicate row into 2D matrix
%from now on, whenever you were using xi, use newx(i, :)
  2 Comments
Stephen23
Stephen23 on 16 Dec 2019
Edited: Stephen23 on 16 Dec 2019
"maybe we should create a FAQ focussed just on numbered variables rather than eval)"
Errrr... that is rather the point of my tutorial. Take a look at the examples right at the top: these are not specific to eval, nor do I state that they are.
I specifically wanted to avoid "just another" discussion of eval, which is why I intentionally looked at the disadvantages of load-ing directly into the workspace, assignin, and various ways that (numbered or otherwise) variables can be dynamically accessed. Of course eval is mentioned because it happens to be one of the common ways that beginners create variable names and tends to be a popular search term, but eval is by no means the focus.
I am curious what your proposal means in practice: what would a discussion about numbered variables look like, if it did not mention a) the ways that they can be created/accessed, and b) the disadvantages of doing so? What other topics do you feel should be covered, that are specific to numbered variables?
Guillaume
Guillaume on 17 Dec 2019
Don't get me wrong, your tutorial is great and I'm no way belittling it, such a tutorial is needed. However, for questions like this I feel it's too focussed on eval itself. Wanting to use eval or dynamic variable names is one of the symptom but not the problem in itself. The problem is the desire to have numbered variables or variables named in any sort of pattern. For questions like this, I feel the emphasis should be on the fact that a) matlab already has a built-in way to index variables (normal matrix or cell array indexing) and b) any alternative would be more complicated.
On a side note, I find it interesting that the way people are taught/use matalb lead them to think about numbered variables. It's not something you'd even think of doing in compiled languages (since it's not even possible).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!