MATLAB Answers

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

1 ビュー (過去 30 日間)
hoque
hoque 2016 年 5 月 7 日
コメント済み: Weird Rando 2016 年 5 月 11 日
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

  0 件のコメント

サインインしてコメントする。

回答 (2 件)

CS Researcher
CS Researcher 2016 年 5 月 7 日
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 件のコメント

hoque
hoque 2016 年 5 月 7 日
Thank you. Actually, I am converting the Matlab code to C code using Matlab coder. The column size is not fixed, so I tried the second (using cell). But it is showing that "Code generation is not supported for variable-size cell arrays" Is there any other solution?
CS Researcher
CS Researcher 2016 年 5 月 7 日
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 2016 年 5 月 8 日
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.

サインインしてコメントする。


Weird Rando
Weird Rando 2016 年 5 月 7 日
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 件のコメント

hoque
hoque 2016 年 5 月 8 日
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".
Weird Rando
Weird Rando 2016 年 5 月 11 日
Sorry I wouldn't know I'm still learning.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by