简体   繁体   中英

Subscripted assignment dimension mismatch error MATLAB

i am trying to run a series of equations by calling certain data points from my matrix over a series of combinations. I am essentially comparing each data set against the others, one at a time, over a series of different errors (the errors are the psivct array).

i am getting a "subscripted assignment dimension mismatch" error for the final line of my code and i don't know what to do about it....? I have done my best to comment out my code for clarity...

it works fine when i am only comparing two sets, but when i add the third, i get this error. Any insight?

Thanks again in advance for any help :)

datamatrix=[674 0 6049 46489;760 180 90735 120980; 650 0 12500 61000]; %data points velocity,
heading, x location, y location
psivct=[-90, -45, 0, 45, 90];%degrees degrees of error 
psidot=3; %degrees/sec rate of error change
d=500; %distance apart for a problem, in feet
Nr_AC=3; %number of aircrafts in sector


for c1 = 1:size(psivct,2)
    psi1=psivct(c1); %for loop to run through array of blunder errors

    for c2=1:Nr_AC-1; %for loop to account for all pairs and avoid same pairs twice

        for c3=c2+1:Nr_AC; %for loop to help avoid account for hte same pairs twice


k1=datamatrix(c2,1).*cosd(datamatrix(c2,2)+psi1)-datamatrix(c2,1)*cosd(datamatrix(c3,2));

k2=((datamatrix(c2,1)/psidot).*(sind(datamatrix(c2,2)+psi1)-sind(datamatrix(c2,2))))
cosd(datamatrix(c2,2)+psi1)*datamatrix(c2,1).*(psi1./psidot)+datamatrix(c2,3)-datamatrix(c3,3);

k3=datamatrix(c2,1).*sind(datamatrix(c2,2)+psi1)-datamatrix((c3),1)*sind(datamatrix((c3),2));

k4=(datamatrix(c2,1)/psidot)*(cosd(datamatrix(c2,2))-cosd(datamatrix(c2,2)+psi1))
sind(datamatrix(c2,2)+psi1)*datamatrix(c2,1).*(psi1./psidot)+datamatrix(c2,4)-datamatrix(c3,4);

k5=k1.^2+k3.^2; %"a" in the polynomial
k6=2*(k1.*k2+k3.*k4); %"b" in the polynomial
k7=k3.^2+k4.^2-500; %"c" in the polynomial
p = [k5 k6 k7]; % creating hte polynomial
r = roots(p); %solving for roots
rr(:,c1,c2,c3)=real(r)/60 %getting real answers
end;
end;
end;

Your problem comes from the r vector: it sometimes is empty because the polynomial doesn't have roots. Then r is a 0x1 vector, and you're assigning it to a 2x1 vector ( r(:,c1,c2,c3) is a 2x1 vector because the first time you assign r to it, Matlab creates the matrix rr and initializes its size).

You would need a check on the existence of roots before you do the assignment. Something like

if (~isempty(r))
  rr(:,c1,c2,c3)=real(r)/60; %getting real answers
end

As a general rule, it would be good to figure out how the size of rr before starting your loops, and initializing rr. It results in better performance, and 'cleaner' code, since Matlab is not trying to resize the 4-D matrix all the time.

Also, be careful in your lines for k2 and k4, the sind and cosd are not taken into account right now. If you wanted to continue the line (and multiply the first line by the second, you should do:

k2=((datamatrix(c2,1)/psidot).*(sind(datamatrix(c2,2)+psi1)-sind(datamatrix(c2,2)))) * ...
cosd(datamatrix(c2,2)+psi1)*datamatrix(c2,1).*(psi1./psidot)+datamatrix(c2,3)-datamatrix(c3,3);

with * or .* depending on what you want to accomplish

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