[Matlab] Subscripted assignment dimension mismatch.

In summary, the conversation is about a program with an error related to mismatched dimensions. The person is trying to solve the problem using (./) but it is not working. They are asking for help in detecting the problem and the conversation includes some code and explanation of the error.
  • #1
s_hy
61
0
Hi all,

I have the following program, but with this error [Subscripted assignment dimension mismatch.

Error in test_vlf_spherical (line 62)
ep(i,j)=ga(i,j)*ep(i,j)+gb(i,j)*((1./r(i)./dr)*(rph*hr(i,j)-rmh*hr(i-1,j))...]

i am trying to solve the problem by using (./) but i didnt work. can anyone help me to detect the problem? thank you

Code:
close all
clear all

k=200;
T=500;

c=3*10^8;
f=3*10^9;
lamda = c/f;
omega = 2*pi*f;

%ddx = lamda/20;
rr = 201;
dr = 0.05*lamda;
dt = dr/(2*c);
r = 0:dr:dr*(rr-1);

u0=4*pi*1e-7;
eps = 1/(4*pi*9*10^9);  
sigma = 0;  
       
t0 = 40;                

%initialization
ep = zeros(k+1,k+1);
ht = zeros(k+1,k);
hr = zeros(k,k+1);

%ionospheric profile for earth-ionosphere waveguide as coefficients
omegap = 1;
epsr   = 1;
sigmar = 1;

%property coefficient
for i=1:k                                  
    for j=1:k;
        ga(i,j)=exp(-(sigmar*dt)/(eps*epsr));  
        gb(i,j)=(1./sigmar)*(1-exp(-(sigmar*dt)/(eps*epsr)));              
    end;
end;


%define theta
dth(2) = pi/180;

 % update equations
 for t=1:T        
      % source
      %pulse=exp((-0.5)*( (t0-t)/spread ).^2);
      pulse=sin(2*pi*f*t*dt);
      ep(1,:)=pulse;
     
   
    %  update Ez field
    for i=2:k
         for j=2:k
            rph = r(k)+dr/2;
            rmh = r(k)-dr/2;
            ep(i,j)=ga(i,j)*ep(i,j)+gb(i,j)*((1./r(i)./dr)*(rph*hr(i,j)-rmh*hr(i-1,j))...
                -(1./r(i)./dth)*(ht(i,j)+ht(i,j-1)));
         end
    end
     %
      for j=1:k+1
          ep(1,j)=0;
          ep(i+1,j)=0;
      end      
      for i=1:k+1
          ep(i,1)=0;
          ep(i,k+1)=0;
      end      
      %  
         
    %update Ht (theta) field =Hx
    for i=1:k+1
          for j=1:k
              rph = r(i)+dr/2;
              rmh = r(i)-dr/2;
              ht(i,j) = ht(i,j)+(dt/u0/r(i)/dr)*(rph*ep(i,j)-rmh*ep(i,j+1));
         end
    end      

     % update Hr field =Hy
      for i=1:k    
        for j=1:k+1
            hr(i,j) = hr(i,j)+(dt/u0/r(i)/dth/sin(dth))*(sin(dth(i+1))*ep(i+1,j)...
                -sin(dth)*ep(i,j));
        end
    end
   
    % plot
    mesh(ep)
    tm=['T=',num2str(t)]
    text(10,100,0.5,tm)
    axis([1 101 1 101 -1 1]);
    drawnow;
         
end
 
Physics news on Phys.org
  • #2
This error occurs when you reference a group of elements and try to do something that mismatches the size.

For example, if you try to assign a 2 element vector as a single element in a matrix:

A = magic(3);
A(1,1) = [1,2]

Subscripted assignment dimension mismatch.

So I recommend you go through your code and track the sizes of each variable to find the error.
 

Related to [Matlab] Subscripted assignment dimension mismatch.

What does "Subscripted assignment dimension mismatch" mean in Matlab?

Subscripted assignment dimension mismatch is an error that occurs when the dimensions of the arrays being assigned do not match. This can happen when trying to assign a value to an array using a subscript that is out of range, or when trying to assign a value to a portion of an array that is not the same size as the value being assigned.

How do I fix a "Subscripted assignment dimension mismatch" error in Matlab?

To fix this error, you will need to ensure that the dimensions of the arrays being assigned are compatible. This may involve resizing one of the arrays or using a different subscript to access the array elements. You can also use the "size" function in Matlab to check the dimensions of the arrays and make any necessary adjustments.

Why am I getting a "Subscripted assignment dimension mismatch" error when trying to assign a value to an array?

This error can occur if the dimensions of the array being assigned do not match the dimensions of the value being assigned. For example, if you are trying to assign a 3x3 matrix to a 2x2 matrix, the dimensions will not match and you will get this error.

Can I prevent "Subscripted assignment dimension mismatch" errors in Matlab?

While it is not possible to completely prevent this error from occurring, you can minimize the chances by carefully checking the dimensions of arrays before attempting to assign values. Additionally, using the "size" function to check dimensions and using loops or built-in functions to resize arrays can help avoid this error.

Is there a way to automatically handle "Subscripted assignment dimension mismatch" errors in Matlab?

Yes, you can use a "try-catch" block to catch this type of error and handle it in your code. This allows you to specify what action to take when the error occurs, such as displaying a warning message or reassigning the value to a differently sized array. However, it is generally better to address the root cause of the error rather than relying on try-catch blocks.

Similar threads

  • Programming and Computer Science
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Linear and Abstract Algebra
Replies
1
Views
818
  • Atomic and Condensed Matter
Replies
3
Views
1K
Replies
8
Views
845
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Calculus and Beyond Homework Help
Replies
2
Views
356
Back
Top