Subscripted assignment dimension mismatch MATLAB - matlab

I am unsure why I get the error. Can anyone help please
L = [];
indexgood=1;
load mrsgarch_t2
eval(['L(:,',num2str(indexgood),') = Loglike(:);']);
indexgood=indexgood+1;
The error I get:
Subscripted assignment dimension mismatch
thanks

Appernetly, the size (and dimensionality) of L(:,indexgood) is different than the size of Loglike(:).
Is it possible that there is a saved variable L in the file mrsgarch_t2.mat?
You will get this error if the number of rows in L is different than the number of elements in Loglike.

From the code and other comments/answers, my guess is either that L is still empty, which could return that error when you try and access L(:,1), or that logLike(:) is not returning nice 1-d values.
Do us a favor and do: disp(L);pause before you evaluate it and see if its what you were expecting. If you could give us the class and dimensions of logLike and L once its loaded, that would be great.
Additionally, if you are loading L every time, setting L to [] is redundant and can be removed.

Related

Appending a row/column of different size to an array.

Recently, I came across this behaviour in Matlab and I am curious as to know why this happens.
a(1,:) = rand(4,1);
a(2,:) = rand(5,1);
This throws me Subscripted assignment dimension mismatch. error whereas
a(1,:) = rand(4,1);
a(2,1:5) = rand(5,1);
adjusts the array to the max column size and appends zero to the shorter ones.
My question is: Why the former code snippet doesn't do what the latter does(which seems very logical to me)? I don't see any reason as to why the former code snippet should work the way it does. Or am I missing something?
With a(2,:) = rand(5,1);, you are attempting to assign a 5x1 vector to a 4x1 matrix column. Hence the error.
With a(2,1:5) = rand(5,1);, you are explicitly referencing a 5th row, which tells the Matlab engine to expand the matrix accordingly before attempting the assignment operation. Hence, this will succeed.
The way I see it, trying to put 5 values into a 4 row vector likely comes from an error in the code, so the user has to explicitly state that this is what he intends to do. Of course, I can only infer on the intent of Mathworks developers when they specifiy the behavior of their language.

Subscripted assignment dimension mismatch

here is the code listing and i got the above mentiond error at line nests(r,c)=nests(r,c)+stepsize.*randn(size(nests(r,c))); please let me now what is wrong with my code as i m new to matlab
for r = 1:numb_of_nest % for each particle
for c = 1:4
u=randn(size(nests(r,c)))*sigma;
v=randn(size(nests(r,c)));
step=u./abs(v).^(1/beta);
nests(r,c)=nests(r,c)+stepsize.*randn(size(nests(r,c)));
% Apply simple bounds/limits
ns_tmp=nests(r,c);
I=ns_tmp<Lb(c);
ns_tmp(I)=Lb(I);
% Apply the upper bounds
J=ns_tmp>Ub(c);
ns_tmp(J)=Ub(J);
% Update this new move
nests(r,c)=ns_tmp;
end
end
This error happens when you assign a value of some dimension m x n to a subscripted variable of different dimension.
In your case, assuming nests has no third dimension, you're assigning to a scalar (1x1) variable. This only works if the value you're trying to assign also is a scalar. Since you get the error, it probably isn't. The only place where your dimensions can be non-scalar is stepsize, so to fix this error, make sure stepsize is a scalar value.
According to the definition you gave in an earlier comment (stepsize=0.01*step.*(nests(r,c)-best);), this problem translates to make sure best is a scalar value. Possibly by subscripting, I can't tell you exactly how since I don't know what best is.
step=u./abs(v).^(1/beta);
nests(r,c)=nests(r,c)+stepsize.*randn(size(nests(r,c)));
Here you're assigning a value to the variable step, but then using a different variable called stepsize that hasn't been assigned a value anywhere in this code. Is this intentional? If not, stepsize is probably some leftover variable from previous code which is messing up the dimensions and giving you this error.
In addition to the above, is nests an ordinary two-dimensional matrix in your code? If so, taking size(nests(r,c)) every time is unnecessary - since you're giving two subscripts, the result is only going to be 1 all the time. Or is nests a cell array perhaps? In that case, you might want to index using curly braces { } instead of ordinary parantheses, to get the size of the matrix that's sitting inside the cell.

What does this Pdesurf error message mean?

I run
pdesurf(mesh.p, mesh.t, u)
I got
Error using pdesurf (line 25)
Illegal solution format.
PDESURF expects input of the form pdesurf(p,t,u). u must either be a column vector and the same length as p, or a row vector and the same length as t. I don't know how big your mesh.p and mesh.t variables are, so I can't say for sure, but it could be because you need to transpose your vector. It is possible that the error might be corrected by changing your code to
pdesurf(mesh.p, mesh.t, ufun(0:0.01:1,0:0.01:1)') % Note the transpose
If this doesn't work, then you need to make sure that either
size(t,2)==size(u,2)
or
size(p,2)==size(u,1)
Transpose u
pdesurf(mesh.p, mesh.t, u')

Passing Struct elements of variable lengths into a matrix Matlab

I am parshing a few data via the internet. The struct Data has several elements. I am interested in Data.Value a call of Data(1,1).Value is a double vector of [56,1]. Moving on to the second struct cell Data(1,2).Value is a double vector [46,1].
Writing a FOR Loop to get the entire Data(1,i).Value from 1 to 500, when it come to the second element, I get the following error returned: Subscripted assignment dimension mismatch.
Although I understand the error I cannot justify it and hence I cannot work out a solution.
I have also tried to pre-define a matrix of variable sizes to overcome this, without result.
Anybody can think of any solution to get the entire Data(1,:).Value
Thanks a lot for the contribution guys.
You can use
vertcat(Data(1,:).Value)
to create a column vector made by concatenating Data(1,1).Value, Data(1,2).Value, ...
Alternatively, you can use the generalized concatenation operator
cat(1, Data(1,:).Value)

error message on the dimensions of matrix in Matlab

When I run this program, I get the error message:
??? Index exceeds matrix dimensions.
Error in ==> if a(1,i)==0
could you tell me why??
a = randi(5,4,100)-ones(4,100);
[n m]=size(a);
for i=1:m
if a(1,i)==0
a(:,i)=[];
end
end
The reason is that you are removing columns from your matrix, so inside the for loop you are reducing its dimension. Then you try to access a column with an index which refers to the original matrix, before the columns were removed.
Try this instead:
a = randi(5,4,100)-1;
ind2remove = (a(1,:) == 0);
a(:,ind2remove) = [];
You get that error because during the execution of this for loop, you might remove some columns. Therefore the dimensions of the matrix will decrease and you will try to access elements that have been moved to a different place.
To do wht you want, you either have to write a while loop, keeping the indices in check manually. The other solution is to vectorize your solution as itamar Katz has shown. That solution is more MATLAB-esque than writig a while loop.
But I have noticed that allowing a random algorithm to emit vectors of random length can sometimes prove more difficult to handle than fixed-length vectors. So you might want to construct your vector in such a way that you don't even have to remove such entries, depending on your application this might be accomplished by generating the first row and other rows with different instructions.