please help! how can i solve the error 'Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
8 views (last 30 days)
Show older comments
I replaced the original code with the data input of the problem as below:( i will attach the file) and i get the problem and i dont know what to do because this is my first time in using matlab to solve problem. i think the problem is when i replace the the "m(i,j)=randint(1,1,9);" with the data input(30x30 matrix).please help me, thank you!
clc;
clear all;
disp('DIJKISTRA ALGORITHM');
n=input('Enter the no. of nodes: ');
for i=1:n
%initialize
for j=i:n
if(i==j)
m(i,j)=0;
else
m(1,j)=[0 39 22 59 54 33 57 32 89 73 29 46 16 83 120 45 24 32 36 25 38 16 43 21 50 57 46 72 121 73;
39 0 20 20 81 8 49 64 63 84 10 61 25 49 81 81 58 16 72 60 78 24 69 18 75 88 68 44 83 52;
22 20 0 39 74 18 60 44 71 73 11 46 6 61 99 61 37 10 51 40 59 5 62 7 57 78 51 51 100 56;
59 20 39 0 93 27 51 81 48 80 30 69 45 32 61 97 75 31 89 78 97 44 83 38 84 100 77 31 63 42;
54 81 74 93 0 73 43 56 104 76 76 77 69 111 72 46 56 84 49 53 33 69 12 69 64 7 69 122 73 114;
33 8 18 27 73 0 45 61 71 88 8 63 22 57 87 77 54 18 68 56 71 20 61 13 75 80 68 52 90 60;
57 49 60 51 43 45 0 85 88 115 52 103 60 75 64 85 79 63 83 78 70 58 38 52 103 49 102 81 69 92;
32 64 44 81 56 61 85 0 74 43 55 23 40 81 97 17 8 50 8 7 23 41 53 48 19 53 17 70 92 63;
89 63 71 48 104 71 88 74 0 38 69 51 75 16 35 75 77 61 77 80 90 76 116 76 58 98 57 19 33 16;
73 84 73 80 76 88 115 43 38 0 81 28 72 53 55 38 49 70 42 50 53 75 83 80 24 69 27 49 51 39;
29 10 11 30 76 8 52 55 69 81 0 55 16 57 91 71 48 11 62 50 68 14 64 9 67 81 61 49 93 56;
46 61 46 69 77 63 103 23 51 28 55 0 44 59 81 32 26 46 29 29 45 47 76 53 15 73 9 49 77 40;
16 25 6 45 69 22 60 40 75 72 16 44 0 67 105 56 33 16 46 35 53 2 57 9 54 72 48 57 106 60;
83 49 61 32 111 57 75 81 16 53 57 59 67 0 39 88 82 51 87 85 103 67 113 65 70 109 67 12 39 19;
120 81 99 61 72 87 64 97 35 55 91 81 105 39 0 84 104 90 93 104 90 104 82 99 79 70 82 50 4 51;
45 81 61 97 46 77 85 17 75 38 71 32 56 88 84 0 23 67 9 21 15 57 48 64 19 41 23 80 81 70;
24 58 37 75 56 54 79 8 77 49 48 26 33 82 104 23 0 44 14 3 25 34 51 41 25 54 23 70 100 65;
32 16 10 31 84 18 63 50 61 70 11 46 16 51 90 57 44 0 58 47 67 16 72 15 59 88 52 42 90 47;
36 72 51 89 49 68 83 8 77 42 62 29 46 87 93 9 14 58 0 12 16 48 48 55 19 45 21 77 89 69;
25 60 40 78 53 56 78 7 80 50 50 29 35 85 104 21 3 47 12 0 22 36 48 43 26 51 24 73 100 68;
38 78 59 97 33 71 70 23 90 53 68 45 53 103 90 15 25 67 16 22 0 54 33 59 33 31 37 93 88 84;
16 24 5 44 69 20 58 41 76 75 14 47 2 67 104 57 34 16 48 36 54 0 57 7 56 72 50 57 105 61;
43 69 62 83 12 61 38 53 116 83 64 76 57 113 82 48 51 72 48 48 33 57 0 57 66 18 69 113 84 115;
21 18 7 38 69 13 52 48 76 80 9 53 9 65 99 64 41 15 55 43 59 7 57 0 63 74 57 57 101 61;
50 75 57 84 64 75 103 19 58 24 67 15 54 70 79 19 25 59 19 26 33 56 66 63 0 59 7 61 74 52;
57 88 78 100 7 80 49 53 98 69 81 73 72 109 70 41 54 88 45 51 31 72 18 74 59 0 64 117 71 107;
46 68 51 77 69 68 102 17 57 27 61 9 48 67 82 23 23 52 21 24 37 50 69 57 7 64 0 57 77 48;
72 44 51 31 122 52 81 70 19 49 49 49 57 12 50 80 70 42 77 73 93 57 113 57 61 117 57 0 49 11;
121 83 100 63 73 90 69 92 33 51 93 77 106 39 4 81 100 90 89 100 88 105 84 101 74 71 77 49 0 48;
73 52 56 42 114 60 92 63 16 39 56 40 60 19 51 70 65 47 69 68 84 61 115 61 52 107 48 11 48 0];
m(j,i)=m(i,j);% equal upper and lower triangular matrix
end
end
end
disp(m);
i=1;
x=1;
temp=triu(m); %for coloring of the lines after computing path
for i=1:n
for j=1:n
if(temp(i,j)~=0)
lc(i,j)=x; %just numbering in sequence for line count
lc(j,i)=lc(i,j);
x=x+1;
end
end
end
bg=biograph(triu(m),[],'showarrows','off','ShowWeights','on','EdgeTextColor',[0 0 1]);
view(bg);
over=0;
while(over==0)
from=input('Enter the source node no.: '); %get inputs
to=input('Enter the destination node no.: ');
v=zeros(1,n);%visited matrix
d=randint(1,n,100);%distance to node matrix
p=zeros(1,n);%previous node matrix
s=from;
v(s)=1;%mark from as visited and initialixe d and p with 0
d(s)=0;
p(s)=0;
stop=0;
while(stop~=n)
stop=0;
for i=1:n
if(v(i)~=1&&m(s,i)~=0)
if(d(s)+m(s,i)<d(i))%if smaller than previous value replace
d(i)=m(s,i)+d(s);
p(i)=s;
end
else
stop=stop+1;
end
end
tempmat=d;
for i=1:n
if(v(i)==1)
tempmat(i)=100;%to find the next minimum
end
end
[k z]=min(tempmat);
s=z;% next minimum node as source node
v(s)=1; % mark as visited
end
%%%%%%%%%%%%%%%%%%%%%%%%%computation over%%%%%%%%%%%%%%%%%
no=1:n;
fprintf('\nNodes: ');
disp(no);
fprintf('Visited: ');
disp(v);
fprintf('Previous:');
disp(p);
i=1;
out=2;
dest(1)=to;
while(i==1) %backtrace from destination to source
to=p(to);
dest(out)=to;
out=out+1;
if(to==from)
i=0;
end
end
bg=biograph(triu(m),[],'showarrows','off','ShowWeights','on','EdgeTextColor',[0 0 1]);
for i=1:out-1
set(bg.nodes(dest(i)), 'color', [1 0 0]);
set(bg.nodes(dest(1)),'color',[0 1 0]);
if(i<out-1)
set(bg.edges(lc(dest(i+1),dest(i))),'linecolor',[1 0 0]);
end
end
view(bg);
sample=input('Check Minimum distance for next instance y/n: ','s');
if(sample=='n')
over=1;
end
end
14 Comments
dpb
on 9 Jul 2017
"still i dont get why that error always appears"
I don't know what the code is intended to do but you've got the indexing expression
bg.edges(lc(dest(i+1),dest(i)))
where the array lc is set in the nested loop up at the top.
I took your code and ran it to the end of that loop and got the following info for it --
>> n % check on n; looks ok...
n =
30
>> whos lc % ditto for lc size, shape
Name Size Bytes Class Attributes
lc 30x30 7200 double
>> sum(isfinite(lc(:))) % check we've got good values
ans =
31
>>
And there's the problem; only 31 out of 900 values are actually even numbers, the rest are:
>> sum(isnan(lc(:))) % look for NaN left...
ans =
869
>>
Unless you pick only those i in the loop that have dest(i+1) one of those 31 positions for which lc isn't NaN, your code is going to error because you'll have the result after the array reference of
lc(NaN,dest(i))
which is going to error every time with exactly the error message you've got.
Set a breakpoint in the editor and run the code and see what the values of i, dest(i+1) and then revealingly what lc(dest(i+1)) are and I'll wager you'll see the cause of the error.
Precisely what you're trying to do and how to fix the logic I'll leave as "exercise for the student" not being familiar enough with the particular algorithm without further looking to see what it actually is.
Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!