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

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

답변 (2개)

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

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?
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.
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.

댓글을 달려면 로그인하십시오.

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

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".
Sorry I wouldn't know I'm still learning.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2016년 5월 7일

댓글:

2016년 5월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by