How to declare a variable that changes it's size after each loop iteration?

5 views (last 30 days)
I want to declare/pre-allocate two variables "delay_points_local","CFO_points_local" that change the sizes. How can I declare it? Here is the code:
pdf_tau = step_CFO*sum(g_prime_bar_all_relays(:,:,k),1);
[~,tau_k_pos] = max(pdf_tau);
arg_max_tau(k) = delay_points(1,tau_k_pos);
pdf_CFO = step_tau*sum(g_prime_bar_all_relays(:,:,k),2);
[~,CFO_k_pos] = max(pdf_CFO);
arg_max_CFO(k) = CFO_points(1, CFO_k_pos);
step_CFO = 0.0001;
step_tau = 0.01;
rho_g = 20;
for k = 1 : K
delay_points_local(k,:) = arg_max_tau(k) - 0.3 : step_tau : arg_max_tau(k) + 0.3;
CFO_points_local(k,:) = arg_max_CFO(k) - 0.005 : step_CFO : arg_max_CFO(k) + 0.005;
[g_prime_bar_all_relays_local(:,:,k)] = Importance_function_grid( y_rd , CFO_points_local(k,:) , delay_points_local(k,:) , L , os_factor , K , TrainingSequence(k,:) , rho_g , step_tau , step_CFO,roll_off);
end

Answers (2)

CS Researcher
CS Researcher on 7 May 2016
I am not completely sure I understand your question but you can do it two different ways:
If the number of columns are fixed:
delay_points_local = zeros(K,N);
for k = 1:K
delay_points_local(k,:) = ... (the way you are doing it)
end
If you are not sure of the column size
delay_points_local = cell(1,K);
for k = 1:K
delay_points_local{1,k} = ...
end
Hope this helps!
  3 Comments
CS Researcher
CS Researcher on 7 May 2016
How much does the number of columns change with each iteration? Is the change fixed or variable? If you know the max possible column size you can declare the matrix using that size.
hoque
hoque on 8 May 2016
The change is variable. I declared it with max column size. With that max column size, I could generate the C code successfully but the Matlab code shows error showing the message that "Subscripted assignment dimension mismatch". Is that means that the generated C code will work wrong? My work starts from C code.

Sign in to comment.


Weird Rando
Weird Rando on 7 May 2016
Dunno if this code would run. Basically I modify the for loops and added two variable (temp_delay_points_local, temp_CFO_points_local). The delay_points_local and CFO_points_local will contain 0 padding due to the uneven sizes of the array. It may be better to store them in a cell.
pdf_tau = step_CFO*sum(g_prime_bar_all_relays(:,:,k),1);
[~,tau_k_pos] = max(pdf_tau);
arg_max_tau(k) = delay_points(1,tau_k_pos);
pdf_CFO = step_tau*sum(g_prime_bar_all_relays(:,:,k),2);
[~,CFO_k_pos] = max(pdf_CFO);
arg_max_CFO(k) = CFO_points(1, CFO_k_pos);
step_CFO = 0.0001;
step_tau = 0.01;
rho_g = 20;
for k = 1 : K
temp_delay_points_local = arg_max_tau(k) - 0.3 : step_tau : arg_max_tau(k) + 0.3;
delay_points_local(k,1:numel(temp_delay_points_local)) = temp_delay_points_local;
temp_CFO_points_local = arg_max_CFO(k) - 0.005 : step_CFO : arg_max_CFO(k) + 0.005;
CFO_points_local(k,1:numel(temp_CFO_points_local)) = temp_CFO_points_local;
[g_prime_bar_all_relays_local(:,:,k)] = Importance_function_grid( y_rd , CFO_points_local(k,:) , delay_points_local(k,:) , L , os_factor , K , TrainingSequence(k,:) , rho_g , step_tau , step_CFO,roll_off);
end
  2 Comments
hoque
hoque on 8 May 2016
Thank you. The Matlab code run successfully but the C code generator still shows the error message that "the delay_points_local" and "temp_delay_points_local variable" need to be assigned".

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!