## For Loop runs too many times

Tico

### Tico (view profile)

さんによって質問されました 2012 年 12 月 8 日
Hi everyone.. I'm new to matlab and am trying to populate a 3D array by the results of a loop:
for j=1:4
A=randi([0 1],2,3);
C(:,:,j)=A
end
And I am getting different 2x3 C matrices where j=1, 2, then j=1,2,3 then j=1,2,3,4. Is there a way I can run the loop just for j=1, then j=2, thenj=3 and j=4? I thought I should be getting a 3D array containing just 4 "pages" (i-e, 4 matrices??
Thnx..

#### 0 件のコメント

サインイン to comment.

## 2 件の回答

2012 年 12 月 9 日

2012 年 12 月 9 日

I am confused about your question. You say you want to have C be a 2-by-3-by-4 array at the end of the loop, correct? That is exactly what you have at the end of your loop!
>> clear all
for jj = 1:4
A=randi([0 1],2,3);
C(:,:,jj)=A; % Semicolon!!
end
whos C % This tells us what we have in C.
Name Size Bytes Class Attributes
C 2x3x4 192 double
Notice that C is the exact size you want. I wonder if you got confused by leaving off the semicolon so that while the loop was running and so you saw C being built-up for every iteration? This is just one reason to not forget to put a semicolon on your lines of code! There is no practical reason to watch all that screen-dump!
Also, why not just do:
C = randi([0 1],2,3,4); % No loop needed...

Tico

### Tico (view profile)

2012 年 12 月 9 日
Hi Matt.. Thnx for your answer. The thing is when I run the loop, I get the following output:
C =
1 1 1
1 0 0
C(:,:,1) =
1 1 1
1 0 0
C(:,:,2) =
0 0 1
1 0 1
C(:,:,1) =
1 1 1
1 0 0
C(:,:,2) =
0 0 1
1 0 1
C(:,:,3) =
0 0 1
1 1 0
C(:,:,1) =
1 1 1
1 0 0
C(:,:,2) =
0 0 1
1 0 1
C(:,:,3) =
0 0 1
1 1 0
C(:,:,4) =
1 1 0
1 1 0
And also, I need the loop coz I am trying to simulate a game and I have to populate the 3d array n times. It's a really long script (probably badly implemented) but I don't how to do it without a loop. I need to populate the array n times, then compare the rows of each individual matrix with random numbers..

2012 年 12 月 9 日
Exactly! Did you read what I wrote??? Those outputs are you seeing the array being built up during the loop, not the final array. Put a semicolon on there (or copy and paste the code I gave you) and you will not see that awful mess any more....
Slow down and read what was written!

サインイン to comment.

2012 年 12 月 8 日

### Azzi Abdelmalek (view profile)

2012 年 12 月 9 日

C=zeros(2,3,4) % preallocate makes your code faster
for jj=1:4
A=randi([0 1],2,3);
C(:,:,jj)=A
end
The result is one matrix 2x3x4
% avoid using i or j (used to represent imaginary numbers)

Tico

### Tico (view profile)

2012 年 12 月 9 日
Thx Azzi but when I ran your code I still get more than 4 matrices. And some of them are just zeros matrices. That said, I have been digging around and I think I have a solution. All I have to do is set the step value to 4.. The code below seems to work.
for j=1:4:4
A=randi([0 1],2,3);
C(:,:,j)=A
end
But when I try it using for i=1:n:n I am getting n-1 zeros matrices and one random matrix.. Any help?
Azzi Abdelmalek

### Azzi Abdelmalek (view profile)

2012 年 12 月 9 日
do you want
clear
k=0;
for j=1:4:4
k=k+1
A=randi([0 1],2,3);
C(:,:,k)=A
end
but for k=1:4:4 gives you just k=1

サインイン to comment.

Translated by