how to cross over between 10 matrix randomly ?

1 Ansicht (letzte 30 Tage)
Firas Al-Kharabsheh
Firas Al-Kharabsheh am 2 Mai 2016
Bearbeitet: Stephen23 am 2 Mai 2016
if i have
a1 = [ 1 1 0
0 1 0
0 0 1 ]
a2 = [ 1 0 1
0 0 1
0 1 1]
a3 = [0 1 0
0 1 1
1 0 1 ]
.
.
.
a10 = [1 1 0
0 1 0
1 0 1 ]
  1 Kommentar
Stephen23
Stephen23 am 2 Mai 2016
Bearbeitet: Stephen23 am 2 Mai 2016
Don't.
Put all of your matrices in one array (ND-numeric, cell, structure,..., whatever), and then use indexing to access each of those matrices.
Yes, this is really the best solution (no matter how much beginners love to think that accessing variables names dynamically is a great idea, it isn't).
PS: the best solution would be for you to use one 3D array, and simply loop over the third dimension:
A = NaN(3,3,10);
A(:,:,1) = [1,1,0;0,1,0;0,0,1];
A(:,:,2) = [1,0,1;0,0,1;0,1,1];
A(:,:,3) = [0,1,0;0,1,1;1,0,1];
...
A(:,:,10) = [1,1,0;0,1,0;1,0,1];ú
% loop over the third dimension:
for k = 1:size(A,3)
A(:,:,k)
end
That is it, it really is that simple. This is much much faster and more robust than any hack code you could write to try access ten separate variables. Read my Answer for more information.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 2 Mai 2016
Ugh... this is a really bad idea!
Avoid creating dynamically named variables in MATLAB. This is poor practice as has been explained many times on this forum, and is not recommended by TMW/MATLAB themselves:
When you are a beginner it seems like a cunning and fast way to store information, but actually it is really bad practice to name your variables dynamically. MATLAB is also not intended for this kind of variable naming: if you continue to include data in the variable names then you will find yourself fighting many more of these battles against MATLAB.
However when you use more appropriate storage for your data (and meta-data) then you will suddenly find lots of MATLAB functions that do many useful operations for you, quickly and easily.
In your case a much more robust solution would be to use:
There are many functions that support working on structures and cell arrays and can access their data easily, and many functions operate on complete numeric arrays all at once without any loops (i.e. vectorized code, which is something you need to learn about).
Placing your data in a structure or cell array also makes it much easier to pass to functions: can you imagine the fight you would have trying to pass hundreds of dynamically named variables to a function?
If you have a newer version of MATLAB you can also use a table , which stores the data together in one array but also allows key-name access to the columns. This might be a good alternative for your data.
Not only that but using the function eval makes your code very slow, extremely difficult to debug, and it obscure the code's meaning. Beginners often use eval for everything, and then complain that they cannot debug it, or that their code is very slow:
Solution: do not use eval! It really is that simple.
Here are discussions of why it is a bad idea to include meta-data (such as an index) in a variable name:
And here is a discussion of why it is a really bad idea to make variables magically appear any workspace (even though beginners love doing this):
Finally, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB:
And in case you thought this is a MATLAB restriction, here is the same discussion for some other languages, advising "DO NOT create dynamic variable names":

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by