For loop error: unable to perform assignment because left and right sides have a different number of elements

1 次查看(过去 30 天)
I am struggling to create a forloop which reduces the following task into a few lines of code:
resp1 = squeeze(resp(:,1,:));
resp2 = squeeze(resp(:,2,:));
resp3 = squeeze(resp(:,3,:));
resp4 = squeeze(resp(:,4,:));
resp5 = squeeze(resp(:,5,:));
resp6 = squeeze(resp(:,6,:));
resp7 = squeeze(resp(:,7,:));
imp_level(iter_rep-num_draw_discard,:,:) = resp1;
imp_slope(iter_rep-num_draw_discard,:,:) = resp2;
imp_curv(iter_rep-num_draw_discard,:,:) = resp3;
imp_unrate(iter_rep-num_draw_discard,:,:) = resp4;
imp_pce_ch(iter_rep-num_draw_discard,:,:) = resp5;
imp_tcu_ch(iter_rep-num_draw_discard,:,:) = resp6;
imp_effr(iter_rep-num_draw_discard,:,:) = resp7;
I've tried the following but got an error
variables = {'level', 'slope', 'curv', 'unrate', 'pce_ch','tcu_ch', 'effr' };
range = linspace(1,size(variables,2),7);
% betas_exog is a (324 x 7) dimension matrix
for i = 1:size(betas_exog, 2)
resp_range(i) = squeeze(resp(:,i,:));
imp_variables{i}(iter_rep-num_draw_discard,:,:) = resp(i)
end
I would appreciate your help in fixing the error and creating a workable for loop.
  6 个评论
J. Alex Lee
J. Alex Lee 2020-12-28
Are you just trying to re-order the dimensions? Or would doing that first let you index more easily for whatever you are trying to do?
matlabuser
matlabuser 2020-12-30
i was trying to just create a set of variables as defined above. To clarify, num_draw_discard is a scaler = 3000, iter_rep = 15,000.

请先登录,再进行评论。

采纳的回答

Gaurav Garg
Gaurav Garg 2020-12-29
Hi,
You can plan to use either Structure array, or a Cell array. You could have also constructed variable name from string, however, the potential risks for it have already been pointed out, about which you can also look here.
The error which you are getting is usually caused when the dimensions of the assigner and the assignee variables are not the same/equal.
As the last option, you can look at Map Containers. They are the objects with keys that index to values, and at the same time, the keys need not be integers

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by