Can not figure out my ? Subscripted assignment dimension mismatch-problem

In summary, the conversation is about a problem with MATLAB code resulting in a Subscripted assignment dimension mismatch error. The issue seems to be with the dimensions of the output variables not matching with the returned values from the agendafunc function. Suggestions for troubleshooting include checking the dimensions of the input variables and using debug mode to identify the source of the error.
  • #1
jacobrhcp
169
0
Can not figure out my ? Subscripted assignment dimension mismatch-problem

EDIT: This is Matlab code.

Hello!

I have some MATLAB code and I've run into a problem that I can't seem to be able to solve. I hope that someone might help me find a solution. Here is the error

Code:
? Subscripted assignment dimension mismatch.

Error in ==> simrun at 30
    [agenda(:,i) in out inagenda
    outagenda]=agendafunc(eventlist,tokendistr(:,i),i,inlist,outlist);

Error in ==> main at 5
simrun();

And this is the code(omitted parts that I think are less important:)
Code:
(...)
for i=1:T
    [agenda(:,i) in out inagenda outagenda]=agendafunc(eventlist,tokendistr(:,i),i,inlist,outlist);
(...)    
end
(...)

Here agenda is a 150x600 array, T=600, in and out are doubles, inagenda and outagenda are both 150x1 arrays, eventlist is a 150x600 array, tokendistr is a 150x600 array, inlist and outlist are both 150x600 arrays. popsize=150.

The agendafunc() function is this:
Code:
function[agendatoday in out inagenda outagenda]=agendafunc(eventlist,tokenstoday,dayno,inlist,outlist)
global tokentreshold popsize p3
agendatoday=zeros(popsize,1);
in=0;out=0;
inagenda=zeros(popsize,1);
outagenda=zeros(popsize,1);
for i=1:popsize
    %comments
    if(tokenstoday(i)>tokentreshold)
        if(eventlist(i,dayno)==1)
            agendatoday(i)=1; % comments
            out=out+1;
            outagenda(i)=out;
        else if(eventlist(i,dayno)==2 || eventlist(i,dayno)==0)
                agendatoday(i,dayno)=0; % comments
            else
                agendatoday(i,dayno)=0; % comments
            end
        end
    else if(tokenstoday(i)==0)
            %comments
            if(eventlist(i,dayno)==2)
                agendatoday(i)=2;
                in=in+1;
                inagenda(i)=in;
            else
                a=rand;
                if(p3<a)
                    agendatoday(i)=2;   % comments
                    %comments
                    in=in+1;
                    inagenda(i)=in;
                end
            end
        else if(check(tokenstoday,outlist,inlist,dayno)==true)
                %comments
                if(eventlist(i,dayno)==2)
                    agendatoday(i)=2;
                    in=in+1;
                    inagenda=in;
                else if(eventlist(i,dayno)==1)
                        agendatoday(i)=1;
                        out=out+1;
                        outagenda(i)=out;
                    else if(eventlist(i,dayno==0))
                            a=rand;
                            if(p3<a)
                                agendatoday(i)=2;   % comments,
                                %comments
                                in=in+1;
                                inagenda(i)=in;
                            end
                        end
                    end
                end
            else
                %comments
                if(eventlist(i,dayno)==1)
                    agendatoday(i)=1;
                    out=out+1;
                    outagenda(i)=out;
                else if(eventlist(i,dayno)==2)
                        agendatoday(i)=2;
                        in=in+1;
                        inagenda(i)=in;
                    else
                        agendatoday=0;
                    end
                end
            end
        end
    end
end
end

where I think the most important part is that I never change the size of any array. I also ommitted my comments here.

I have no idea what went wrong, as the array sizes I believe to be of correct size. The error takes place the second run of the loop, i.e. i=2 in the first for loop in the middle code window of this page. Does anyone have a tip for me what I could be missing or why Matlab finds an error?

Thanks a lot.
 
Last edited:
Physics news on Phys.org
  • #2


Hello,

From looking at your code and the error message, it seems like the issue might be with the line:

[agenda(:,i) in out inagenda outagenda]=agendafunc(eventlist,tokendistr(:,i),i,inlist,outlist);

Specifically, it seems like the dimensions of the output variables (agenda, in, out, inagenda, outagenda) are not matching with what is being returned by the agendafunc function. This could be due to a mismatch in the dimensions of the input variables or a mistake in the agendafunc function itself.

I would recommend checking the dimensions of all the variables being passed into the agendafunc function and also double-checking the code within the function to make sure it is returning the correct dimensions.

Another possibility could be that the dimensions of the variables are changing within the for loop, causing the mismatch. I would suggest using the debug mode in MATLAB to step through the code and see where exactly the error is occurring.

I hope this helps. Good luck with your code!
 

What does "subscripted assignment dimension mismatch" mean?

Subscripted assignment dimension mismatch refers to an error that occurs when the dimensions (size or shape) of the arrays or matrices being used in an operation do not match. This can happen when trying to assign values to an array or matrix that is smaller or larger than the one being assigned to.

Why am I getting a "subscripted assignment dimension mismatch" error?

This error typically occurs when trying to perform an operation on arrays or matrices with different dimensions. It can also happen when trying to assign values to an array or matrix that do not match its dimensions.

How can I fix a "subscripted assignment dimension mismatch" error?

To fix this error, you will need to ensure that the dimensions of the arrays or matrices being used in an operation match. You may need to reshape or resize arrays or matrices to make them compatible. Additionally, make sure that the dimensions of the array or matrix being assigned to match the dimensions of the values being assigned.

Can I prevent "subscripted assignment dimension mismatch" errors?

While it is not possible to completely prevent this type of error, you can reduce the likelihood of it occurring by carefully checking and matching the dimensions of arrays and matrices before performing operations or assignments. It can also be helpful to use functions or tools that automatically check and align dimensions, such as broadcasting in MATLAB.

Is there a difference between "subscripted assignment dimension mismatch" and "dimension mismatch" errors?

Yes, there is a difference. "Subscripted assignment dimension mismatch" specifically refers to an error that occurs during an assignment operation, while "dimension mismatch" can refer to any error related to mismatched dimensions, such as in function inputs or matrix operations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Atomic and Condensed Matter
Replies
3
Views
867
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
615
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
Back
Top