How can I append arrays onto the same variable in a series of loops?
조회 수: 25 (최근 30일)
이전 댓글 표시
Hi, I am a beginner in MATLAB and was wondering if someone with more experience would find it easyto do what I am trying to achieve. I have a number of 2x6 matrices that I would like to "append" to one another in the z-dimension in order to make a single 3D variable. I would then like to output this variable as a .mat file. For example, lets say I have 3 different options, and each one has 3 sub-options which yield me a total of 9 different matrices. I had envisioned something like the following, but I can't get it to work. The goal would be to have a single variable that is 2x6x9 Any help is greatly appreciated. Thanks!
My bad example:
x = zeros(2,6);
counter=0;
for i=1:3
counter = counter+1;
for j=1:3
x(:,:,counter)= rand(2,6);
save('test.mat','x','-append');
counter = counter+1;
end
end
JH
댓글 수: 0
답변 (2개)
Stephen23
2015년 5월 5일
편집: Stephen23
2015년 5월 5일
Solution One: Vectorized
Your example uses rand, which lets you generate the whole 3D array directly by specifying the outputs size in a vector, no loops or concatenation is required:
out = rand([2,5,3]);
This will be much faster than using any loop and concatenating arrays, and is a good introduction to vectorized code! However if you really want to concatenate arrays, then read on...
Solution Two: with for-Loop
For a start there is no reason why you need to use the variable counter when you already have the for-loop variable. Why bother? And preallocating the array will make this faster. But the basic issue is that you are trying to concatenate and save the matrix in each loop iteration. Why not just create the whole array, and then save it?:
>> N = 3;
>> X = nan([2,6,N]);
>> for k = 1:N
X(:,:,k) = rand(2,6);
end
>> save('test.mat','X')
Bonus Information on Concatenation
>> A = [1,2;3,4];
>> B = [10,20;30,40];
>> C = [0,NaN;Inf,-Inf];
>> out = cat(3,A,B,C)
out(:,:,1) =
1 2
3 4
out(:,:,2) =
10 20
30 40
out(:,:,3) =
0 NaN
Inf -Inf
Note that this is even easier if you place the variables into a cell array:
>> Z = {A,B,C};
>> out = cat(3,Z{:})
댓글 수: 3
Stephen23
2015년 5월 5일
Sure, attach some graphic / daigram, as this is not clear to me. Did Solution Two not work?
Delladj Kamel
2015년 5월 5일
*It's easy, just type this code and you'll get what you are expecting*
x = zeros(2,6);
for i=1:9
x(:,:,i)= rand(2,6);
end
save('test.mat','x');
results
x(:,:,1) =
0.3724 0.4897 0.9516 0.0527 0.2691 0.5479
0.1981 0.3395 0.9203 0.7379 0.4228 0.9427
x(:,:,2) =
0.4177 0.3015 0.6663 0.6981 0.1781 0.9991
0.9831 0.7011 0.5391 0.6665 0.1280 0.1711
x(:,:,3) =
0.0326 0.8819 0.1904 0.4607 0.1564 0.6448
0.5612 0.6692 0.3689 0.9816 0.8555 0.3763
x(:,:,4) =
0.1909 0.4820 0.5895 0.3846 0.2518 0.6171
0.4283 0.1206 0.2262 0.5830 0.2904 0.2653
x(:,:,5) =
0.8244 0.7302 0.5841 0.9063 0.8178 0.5944
0.9827 0.3439 0.1078 0.8797 0.2607 0.0225
x(:,:,6) =
0.4253 0.1615 0.4229 0.5985 0.6959 0.6385
0.3127 0.1788 0.0942 0.4709 0.6999 0.0336
x(:,:,7) =
0.0688 0.5309 0.4076 0.7184 0.5313 0.1056
0.3196 0.6544 0.8200 0.9686 0.3251 0.6110
x(:,:,8) =
0.7788 0.0908 0.1537 0.4401 0.4574 0.5181
0.4235 0.2665 0.2810 0.5271 0.8754 0.9436
x(:,:,9) =
0.6377 0.2407 0.2891 0.6951 0.2548 0.6678
0.9577 0.6761 0.6718 0.0680 0.2240 0.8444
댓글 수: 2
Stephen23
2015년 5월 6일
This would be significantly improved by using array preallocation, simply by defining the final size of the matrix at the beginning:
x = zeros([2,6,9]);
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!