Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-20.
24 views (last 30 days)
Show older comments
Unable to perform assignment because the size of the left
side is 1-by-1 and the size of the right side is 1-by-20.
15 Comments
Nilanshu Ranjan
on 28 Sep 2022
for i = 1 : s-2
R(i,i) = 1-b0;
R(i+1,i) = b01(t);
R(i+2,i) = b02(t);
end
Here b0 is a 1×20 vector that you are trying to assign to R(i,i) which is a 1×1 type. You perhaps want R to be a 3-D matrix of dimensions s×s×20 to be able to assign b0 to R(i,i) .
If you do not require R to be a 3-D matrix, you may want to use a specific value from the vector b0 as b0(index).
Also see that you have used b01(t) and b02(t) which are 1×1 double types and indexing them with a number greater than 1 will throw error.
Accepted Answer
Walter Roberson
on 6 Nov 2022
t = 1:20; % Number of time steps
That is a vector of size 1 x 20.
b1(t) = b01*(t) + 0.01*(t);
b2(t) = b02*(t) + 0.001*(t);
Since t is 1 x 20 and b01 and b02 and 0.01 and 0.001 are scalars, then b1 and b2 will become scalars of size 1 x 20.
Beware: the b1(t) on the left hand side hints that you are thinking of those two lines as if they are defining formulas relating any input t value to an output value. However, in MATLAB, when you have an indexed variable on the left hand side of the assignment, the only time you can be defining a formula is if you are using the Symbolic Toolbox, such as
syms t
b1(t) = b01*(t) + 0.01*(t)
which would create a symbolic function relating the symbolic variable t to an output.
When t is numeric, as it is in your situation, then b1(t) on output means the same as indexing b1 at the values stored in t. If you had, for example, defined t = linspace(0, 20, 50) then that would include non-integers and assigning to b1(t) would be an error because of the non-integer indices.
You should typically be assigning to an unindexed variable in such numeric situations, such as
b1 = b01 * t + 0.01 * t;
and if you have the case where b1 is already initialized and you want to write to the beginning of it, you should tend more towards.
b1(1:length(t)) = b01 * t + 0.01 * t;
Continuing:
b0 = 1 - b1 - b2; % Density of state changing
with b1 and b2 both being 1 x 20, b0 is 1 x 20.
Then in your loop,
for i = 1 : s-2
R(i,i) = 1-b0;
you appear to be wanting to write 1-b0 into diagonal entries in R. But remember b0 is a 1 x 20 array, and R(i,i) is a single output location. 20 values will not fit into one value.
You have a few possibilities at this point:
- you can select one of the b0 values to use for the calculation. For example, R(i,i) = 1-b0(i) . The difficulty with that is that the maximum value of i is s-2 which is 100-2 which is 98, but b0 only has 20 elements
- you can extend R into the third dimension, writing to R(i,i,:) . Your comments seem to imply that you do not want to do this.
- You could make R into a cell array and write to R{i,i} so that each location could store a 1 x 20 vector. But if you do that, you would have trouble doing mathematics with R
I have to wonder if you are trying to initialize the upper left corner diagnonals of R?? If so then
for i = 1 : length(t)
R(i,i) = 1-b0(i);
R(i+1,i) = b01(i);
R(i+2,i) = b02(i);
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!