how to create a customized array and running a counter through it?

hello every one , i want to create an array with size A[128x4], which has A[1] consisting of only 128 elements, A[2] consisting of 32 elements, A[3] consisting of 8 elements, A[4] consisting of 2 elements, i want to run a (m = 1:256) bit counter through my array in a way that after filling my 128 elements in A[1] it goes onto fill the A[2] ..... i want to collect my output at from second element from A[4] array, it will be even better if the array shift after receiving a incremental feedback((i.e receiving on feedback as '2' the counter goes to A[2] array, receiving feedback as '3' the counter goes to the A[3] array to it an so on)). kindly guide me with it matlab, thank you.

 Accepted Answer

Store your arrays in a cell array. Loop over the cell array:
A = cell(1,5);
for k = 1:numel(A)
A{k} % access the kth array.
... your code
end

10 Comments

N= 256; %%counter size
A = cell(1,4); %% four cell arrays
for m =1:N %%counts
for k = 1:numel(A) %%incremental feedback
A((N/(4^(k-1)))/2)=0; %%when k= 1, it creates 128 bit size, for k =2 ,32bit size,
%%similarly when k =3,8bit,when k= 4, it creates 2 bit size.
A(1) = m; %%giving an input to the A[1] of the first cell
A = circshift(A,[0,-1]); %%shifting operation
output=A; %%output collecting
end
end
these are my thoughts on the code, how to make the cell shift from A[1] to A[2], after all the elements are filled in A[1]??? as shown in the figure??
Exactly like I showed in my answer, you need to access the cells of the cell array using indexing:
A{k}
Your code is not very clear. You loop over 1:N, but never use this index m inside the loop, so only the last one is saved. It is not clear what values you want to save. It is not clear what output is, or why your completely reallocate A on every iteration.
I would suggest that instead of giving us broken code you actually show us the exact inputs and outputs that you need, with exact values and an explanation. Then we have something to write our code with, and to test it on. Broken code does not explain anything. Your explanation is what we need.
k sure sir ill do it, where can i post the entire code here only??
im performing a r2square single delay feedback fft., im doing it using two function programs.
Did you read my comment? I asked for an explanation of this question, not more code. Please tell us (with example numeric data) what you want to do.
Sandeep Kumar's "Answer" moved here (and formatted properly):
N= 16; %%counter size
A = cell(1,2); %% four cell arrays
A{1,1}(8) =0 ;
A{1,2}(2) = 0;
for k = 1:numel(A)
A{k} % access the kth array.
for m=1:16
A{1,1}(1) = m
A{1,1} = circshift(A{1,1},[0,1])
output = A{1,2}(2)
end
end
its filling only A{1,1} not going to A{1,2} after filling the A{1,1} ??
Try changing the index:
A{1,k}
I have already given you a link to the documentation about cell array indexing. Did you read it? I also gave you an example in my answer and in one of my comments. Here it is again:
A{k} % what I showed you
A{1,1} % what you used
Why did you remove the k and replace it with ones? Do you see the difference? Which of these is going to change when your loop variable k changes?
These tutorials are a good way to learn how to use MATLAB, such as how indexing works:
working 256 bit
N= 256; %% counter size
A = cell(1,4); %% four cell arrays
A{1,1}(128) =0 ;
A{1,2}(32) = 0;
A{1,3}(8) = 0;
A{1,4}(2) = 0;
for m=1:256
A{1,1}(1) = m;
A{1,1} = circshift(A{1,1},[0,1]);
A{1,2}(1) = A{1,1}(128);
A{1,2} = circshift(A{1,2},[0,1]);
A{1,3}(1) = A{1,2}(32);
A{1,3} = circshift(A{1,3},[0,1]);
A{1,4}(1) = A{1,3}(8);
A{1,4} = circshift(A{1,4},[0,1]);
output = A{1,4}(2)
end
working 16 bit.
N= 16; %% counter size
A = cell(1,2); %% four cell arrays
A{1,1}(8) =0 ;
A{1,2}(2) = 0;
for k = 1:numel(A)
A{k} % access the kth array.
for m=1:16
A{1,1}(1) = m
A{1,1} = circshift(A{1,1},[0,1])
A{1,2}(1) = A{1,1}(8)
A{1,2} = circshift(A{1,2},[0,1])
output = A{1,2}(2)
end
end
both are working , thanku il see "A{k}" this , how it can be used in my code
oh thanku so much sir, that is even easier to use.
My Pleasure! You can also accept the answer the best resolves your question.

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!