creating multiple copies of an array

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 Commenti

"I would like to create more than one copy of it with different names, for example 5 copies with names x1, x2, x3, x4, x5. how can I do that using a loop ?"
Best not to do that:
Indexing is neat, simple, very efficient, and easy to debug (unlike what you are trying to do). You should just use indexing.
indexing will be hard because I will need to copy it for more than one time and not 5 times most probably I will need to copy it for 5000 times which is hard to do 5000 variables manually. this is why I asked to do it in a loop
Stephen23
Stephen23 il 16 Dic 2019
Modificato: Stephen23 il 16 Dic 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 il 16 Dic 2019
Modificato: ahmed abdelmgeed il 16 Dic 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;

Accedi per commentare.

Risposte (2)

Karthick S
Karthick S il 16 Dic 2019
x = [0 1 0 0 1 1 1 0 1 0];
sz=5000;
for i=1:sz
A{i}=x;
end

9 Commenti

Hi,
I tried that but it is not working, it gives this error:
Unable to perform assignment because brace indexing is not supported for variables of this
type.
Error in (line 4)
A{i}=x;
Stephen23
Stephen23 il 16 Dic 2019
Modificato: Stephen23 il 16 Dic 2019
"I tried that but it is not working, it gives this error:"
Because you already have an existing variable in your workspace with that name. You could clear it, but actually to write robust code the cell array must be preallocated (and this will fix your error message):
x = [0 1 0 0 1 1 1 0 1 0];
N = 5000;
C = cell(1,N); % prealloate!
for k = 1:sN
C{k} = x;
end
Thank you for your replay and helping me but I am not so experienced in matlab And by this way will I be able to access the variables like C1, C2 and so on.
Stephen23
Stephen23 il 16 Dic 2019
Modificato: Stephen23 il 16 Dic 2019
"And by this way will I be able to access the variables like C1, C2 and so on."
No.
By this way you will be able to avoid writing the slow, complex, inefficient, ugly, buggy code that you would have to write to access the variables like that. You're welcome.
ahmed abdelmgeed
ahmed abdelmgeed il 16 Dic 2019
Modificato: ahmed abdelmgeed il 16 Dic 2019
And what if I would like to access the variables ? How can I do that ? I would like to make copies of the array and be able to access the copies.
Stephen23
Stephen23 il 16 Dic 2019
Modificato: Stephen23 il 16 Dic 2019
"And what if I would like to access the variables ?"
Personally I would not do that, nor do I see why you need to.
"I would like to make copies of the array and be able to access the copies."
Sure, that is easy: experienced MATLAB users would use indexing, much like Karthick S showed you four hours ago. Much simpler and more efficient than what you are trying to do (although it should be improved by preallocationg the cell array).
ahmed abdelmgeed
ahmed abdelmgeed il 16 Dic 2019
Modificato: ahmed abdelmgeed il 16 Dic 2019
I want to access the variables because after that I may need to plot the data like plot C1 after that C2 and so on, all on the same graph.
clearly to plot C1 from(0 to 9) and when it finishes plot C2 from (10 to 19) and so on.
actually this data is a like a simple example to what I want to do, as what I want to do exactly I have a waveform data of (0's and 1's) stored in a csv file of length (4064 samples) this 4064 samples would be one block of data and I want to repeat the block continuously which means infinite number of blocks 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.
"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 il 17 Dic 2019
Modificato: ahmed abdelmgeed il 17 Dic 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"

Accedi per commentare.

"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 Commenti

Stephen23
Stephen23 il 16 Dic 2019
Modificato: Stephen23 il 16 Dic 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?
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).

Accedi per commentare.

Richiesto:

il 16 Dic 2019

Commentato:

il 17 Dic 2019

Community Treasure Hunt

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

Start Hunting!

Translated by