for-loop assignment with several dimensions
Show older comments
Hi everybody, I want to compute a value (cross-elasticity) for each product (1st dimension) w.r.t. each other product's price (2nd dimension) in each market (3rd dimension).
All variables are in a panel structure (with market_id and product_id). For the cross-elasticities, I need one dimension more ( since in each market and for each product I compute elasticities w.r.t. other products). The way I imagined the code to work was
cross_elasticity_NL = NaN(number_markets,number_products,number_products);
for i_market=1:number_markets
for i_product = 1:number_products
for j_product = 1:number_products
cross_elasticity_NL( i_market, i_product, j_product) = - beta_2SLS_NL(2).*price(market_id==i_market & product_id == j_product).*log_share(market_id==i_market & product_id == j_product);
end
end
end
But then, I get an assignment problem:
" Assignment has more non-singleton rhs dimensions than non-singleton subscripts" which I don't really get, I have a scalar on the R.H.S. and I am feeding this scalar into one element of a three-dimensional object, which is also a scalar.
Can somebody help me here? Or has a suggestion of how this might work in Matlab? Thanks a lot
2 Comments
I am feeding this scalar
In the code that you have shown, there is nothing that guarantees this.
Does this line causes an error?
assert(size([market_id(:), product_id(:)], 1) == size(unique([market_id(:), product_id(:)], 'rows'), 1))
If so, then the RHS is definitively not scalar for some ids.
Note that if the RHS was indeed scalar, there would be no need for any of the loops.
Accepted Answer
More Answers (0)
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!