How to create arrays from the loop index?

Status
Not open for further replies.

testing test

Member level 3
Joined
Mar 3, 2010
Messages
65
Helped
5
Reputation
10
Reaction score
3
Trophy points
1,288
Activity points
1,656
Hello,

I have numbers from 0 1 2 3 4 5 6 7 and I want to divide it into 4 different arrays of [0 4], [2 6], [1 5] and [3 7]. How does MATLAB initialize arrays within the loop. Assume I don't know the total number of arrays to be created for a generic case where N could be any power of 2.

Code:
N=8;
for i=0:N/2-1
    pair[i]=i:N/2:N-1
end

Please correct the above code. Thank you.
 

I am not sure I understand exactly what you need. Here is my first shot:
Code:
N=8;
for i=0:N/2-1
    pair(i+1,:)=i:N/2:N-1
end

MATLAB needs a positive index and parenthesis not brackets. The ':' means all columns in this case.
 

No, I want to divide an array x=[0 1 2 3 4 5 6 7] into 4 different arrays where a1=[0 4], a2=[2 6], a3=[1 5] and a4=[3 7]. I want to do it using a for loop.
 

The code I gave you divides your array x into 4 arrays called pair(1,, pair(2,, pair(3, and pair(4,.
This works when you do not know the number of 2-element arrays you need. If you know that they are always 4 arrays, you can then add:
a1=pair(1,;
a2=pair(2,;
a3=pair(3,;
a4=pair(4,;

If you see the "smileys", they are " : ) "...
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…