简体   繁体   中英

MATLAB: error Subscripted assignment dimension mismatch

I keep getting the error "Subscripted assignment dimension mismatch" for the x_values line. I've tried switching parenthesis but I can't figure it out. I'm not very good with MATLAB and this code is super long so I didn't want to post the whole thing.

for m = 1:num_part;
    for n = 2:num_steps;
    x_values(m,n) = x_values(m,n-1)+ stride_length .* (cos(step_angle(m,n)));
    y_values(m,n) = y_values(m,n-1)+ stride_length .* (sin(step_angle(m,n)));
    r_values (m,n) = sqrt(x_values(m,n).^2 + y_values(m,n).^2);
    if bound_cross(m)~=0;
        continue;
    elseif bound <= r_values(m,n);
            bound_cross (m,1) = n;
    end
end

As I figured out, error is in the variable bound_cross . If you have defined it in the way you replied in the comment as ' bound_cross = [num_part, 1] ', it creates a 1*2 element matrix only. But if you needed a 20*1 matrix, you have to specifically define it. Jut to check the code, I used an empty matrix and it worked fine.

num_part = 20;
num_steps = 1000;
stride_length = 100;
bound = 1;
bound_cross = ones(num_part, 1) ;
x_values = zeros(num_part, num_steps);
y_values = x_values;
r_values = x_values;
step_angle = x_values;

for m = 1:num_part;
    for n = 2:num_steps;
        x_values(m,n) = x_values(m,n-1)+ stride_length .* (cos(step_angle(m,n)));
        y_values(m,n) = y_values(m,n-1)+ stride_length .* (sin(step_angle(m,n)));
        r_values (m,n) = sqrt(x_values(m,n).^2 + y_values(m,n).^2);
        if bound_cross(m)~=0;
            continue;
        elseif bound <= r_values(m,n);
            bound_cross (m,1) = n;
        end
    end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM