# Calculating a matrix with a variable inside

1 view (last 30 days)
Hi all,
I'm trying to calculate matrix A for different b's. b is defined as follows:
b = logspace(10,1000,100)
Here's the error that i'm getting:
"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
This is what I have and it's clearly not working. Any thoughts or suggestions would be much appreciated.
b = logspace(10,100,1000);
for i = 1:length(b)
A(i) = [exp(b(i))*0.5 , 0;exp(-0.5i*b(i)), b(i)*0.5];
s = det(A)
plot(b,A)
end
Mohammadamin Malek Pour on 17 Apr 2021
@Clayton Gotbergjust updated the question and included the error. thanks

Clayton Gotberg on 17 Apr 2021
Edited: Clayton Gotberg on 17 Apr 2021
The first issue you are having is because you are telling MATLAB to expect a 1x1 answer with A(i) and then providing a 2x2 matrix to fit into that space.
If you need to save 2x2 matrices on each iteration, you can either stack them on top of each other:
A = [0 1 %The first two rows are the results from the first iteration
1 2
3 4 % The second two rows are the results from the second iteration
2 6
... % And so on...
which will cause your indexing to be much more confusing (like A(2*i:2*i+1,:)) or you can use cell arrays, which have indexing more similar to what you are already using and which will make retreiving those matrices much easier later.
A = {[0 1; 1 2],[3 4; 2 6], .. }
% ^ ^ ^
% First iteration 2nd And so on...
To assign to cell arrays, use curly braces around your indexing variable (A{i}). You can later retrieve the data within a cell using curly braces or can retrieve a cell (or set of cells) using brackets.
The second issue you will face is with the det function. Inputs must be square matrices, but A changes size on each iteration and will almost never be square. If this is intended to apply to the matrix from each run, you'll need to change this to address it.
Finally, you need to check that your plot function is in the right place. As it is, you are trying to plot something on each iteration and b and A will usually not be the same size.
##### 2 CommentsShowHide 1 older comment
Clayton Gotberg on 17 Apr 2021
I'm gald I could help! In that case, the trouble with your code is that you are trying to save the matrix after every loop, instead of its determinant.