How can I divide multiple matrices into individual arrays?

1 view (last 30 days)
Good afternoon,
Sorry for bothering you, but I have encountered an issue when trying to separate multiple matrices into individual array. I have 8 matrices, each having a dimension of 500x500, and by wanting to store them separately I mean that there should be 250.000 arrays, each containing 8 values. For instance (using a smaller example):
A = [1, 2, 3; 4, 5, 6; 0 0 0]
B = [3, 2, 1; 6, 5, 4; 0 0 0]
The arrays should be like this:
array1 = [1, 3]
array2 = [2, 2]
array3 = [3, 1] and so forth.
The issue consists of, when using "for" loops, I cannot assign an index to an array, like in C++, for instance, having "array1", "array2" etc., but values being overwritten one over another. I would like, if you can help me in managing how to generate arrays regarding a variable, like so (I am referring at the example above, for better explaining the issue):
for k = 1:2;
for i1 = drange(1:size(A, 1))
for j1 = drange(1:size(A, 2))
for i2 = drange(1:size(B, 1))
for j2 = drange(1:size(B, 2))
v(k) = [A(i1, j1), B(i2, j2)]; %k is not dimension, or position
end
end
end
end
end
I would like "k" to be as an index, creating new arrays of dimension 2 at every iteration.
Thank you for your time and I am waiting for your suggestions!
Best regards,
Alin-Marian Sipica

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 27 Jun 2015
Edited: Azzi Abdelmalek on 27 Jun 2015
A = [1, 2, 3; 4, 5, 6; 0 0 0];
B = [3, 2, 1; 6, 5, 4; 0 0 0];
out=arrayfun(@(x,y) [x,y],A,B,'un',0);
out=out(:);
Now you have 9 array inside the variable out, and you can access each row:
out{1} % fist row
out{2} % Second row
% And so on
  1 Comment
Alin-Marian Sipica
Alin-Marian Sipica on 27 Jun 2015
Edited: Alin-Marian Sipica on 27 Jun 2015
Good afternoon,
Thank you very much for your reply! I have tried your solution and worked as intended for the large dimension matrices. I cannot express my gratitude enough for your help, sir! Believe me when I am saying that I have tried to solve this issue for about 3 hours before posting here. Once again, thank you very much!
Best regards,
Alin-Marian Sipica

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!