for-loop assignment with several dimensions

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 Commenti

Guillaume
Guillaume il 31 Mar 2017
Modificato: Guillaume il 31 Mar 2017
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.
Florestan
Florestan il 31 Mar 2017
Modificato: Florestan il 31 Mar 2017
Thanks for your answer! No, it does not cause an error. So market id and product id together uniquely define each observation. But what do you mean by there is no need for a loop?

Accedi per commentare.

 Risposta accettata

Stephen23
Stephen23 il 31 Mar 2017
Modificato: Stephen23 il 31 Mar 2017
The RHS is empty, because the logical index market_id==i_market & product_id == j_product is all false for some permutation of those values.

3 Commenti

Thanks for your answer! Could you elaborate a bit more why it is false for some permutation of those values? The loop runs across all existing market ids and product ids, how can it be empty?
@Florestan: I looked at your code and thought about how this bug could occur. In contrast you will not learn how to fix bugs if your approach is to find reasons why the code should work (in your mind). Because actually there is an error, therefore something is causing that error, therefore you need to actually look at the what the code is doing, and not at what you believe the code is doing. The code does not care what you want it to be doing (i.e. your explanation). You need to actually look at the code and its outputs, and see what it really is doing.
"how can it be empty"
You can figure this out yourself, here is how you can start:
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
tmp1 = -beta_2SLS_NL(2);
tmp2 = price(market_id==i_market & product_id == j_product);
tmp3 = log_share(market_id==i_market & product_id == j_product);
disp(size(tmp1))
disp(size(tmp2))
disp(size(tmp3))
cross_elasticity_NL( i_market, i_product, j_product) = tmp1.*tmp2.*tmp3;
end
end
end
Again, thanks for your answer. That was a clever way to debug. I learned that the dataset has unbalanced markets (i.e. there are not all products present), so probably this is where the code had an error. So I put some "try catch" around the cross elasticity expression which gets it going, but I am not totally confident with it. Presumably i need to flexibly change the scope of the two last loops such that they only take valid product numbers in each market.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by