Clear Filters
Clear Filters

left and right sides have a different number of elements.

5 views (last 30 days)
Hi, i've this problem
i is a logical 5995x1... price and bubu have equal size
load('matlab_3Variable.mat');
price(i)=bubu;
Unable to perform assignment because the left and right sides have a different number of elements.
  6 Comments
Torsten
Torsten on 6 Jun 2024
Edited: Torsten on 6 Jun 2024
load('matlab_3Variable.mat');
price(i)=bubu(i)
price = 5995x1
1.0e+03 * 1.4645 1.4645 1.4645 1.4645 1.4645 1.4645 1.4645 1.4645 1.4645 1.4538
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
would set the values of price to the values of bubu for which i has logical 1 and leave the other values of price unchanged.
But if it's what you want - we don't know.

Sign in to comment.

Answers (1)

Matlab Pro
Matlab Pro on 6 Jun 2024
This is simple - since the 'i' is a logical indexing vector and is not all 'true'
length(find(i)) % == 5948
ans = 0
Thus: price(i) is a [5948x1] matrix and you want it to hold "bubu" which is 5995 matrix --> impossible
This - can be easliy fixed (if this is your requested outcome) like that;
price(i)=bubu(i);
Unrecognized function or variable 'bubu'.
  2 Comments
Luca Re
Luca Re on 6 Jun 2024
hi, it's not correct because "price" and "bubu" can to have to different size
Stephen23
Stephen23 on 6 Jun 2024
Edited: Stephen23 on 6 Jun 2024
"it's not correct because "price" and "bubu" can to have to different size"
They do not need to be the same sizes:
A = rand(2,3)
A = 2x3
0.3686 0.1659 0.7087 0.4115 0.2469 0.8034
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = rand(4,5)
B = 4x5
0.3748 0.3379 0.6675 0.3304 0.4606 0.1202 0.2557 0.4158 0.0023 0.7938 0.1573 0.9340 0.4140 0.4896 0.3321 0.8504 0.4113 0.6661 0.6588 0.1033
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X = [true,false,true,false,true]
X = 1x5 logical array
1 0 1 0 1
A(X) = B(X)
A = 2x3
0.3748 0.1573 0.3379 0.4115 0.2469 0.8034
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
What is relevant is how many elements on the RHS you are trying to allocate to the LHS.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!