Matlab error for "Assignment has more non-singleton rhs dimensions than non-singleton subscripts".
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
Hi, i have error of "Assignment has more non-singleton rhs dimensions than non-singleton subscripts".
Error in Untitled10 (line 205)
distAllTier1(i,j)= sqrt((node(i).x - node(j).x).^2 + (node(i).y - node(j).y).^2);
The details code as follow:
for i = 1:1:numNodes
for j = indPCHTier1
if node(i).tier == 1
if i == j
distAllTier1(i,j) = NaN;
else
distAllTier1(i,j)= sqrt((node(i).x - node(j).x).^2 + (node(i).y - node(j).y).^2);
end
else
distAllTier1(i,j) = NaN;
break;
end
end
end
The result for
sqrt((node(i).x - node(j).x).^2 + (node(i).y - node(j).y).^2);
is:
ans =
10.295630140987001
21.213203435596427
Then the "distAllTier1(i,j)" cannot support the value. How can i solve this problem?
Thanks :)
Accepted Answer
KSSV
on 27 Sep 2018
sqrt((node(i).x - node(j).x).^2 + (node(i).y - node(j).y).^2);
The output of above is 2*1..and you are trying to save output into a single scalar..
distAllTier1(i,j)
so the error is popping out. I think the output of sqrt() should be a scalar. Show us the complete code to get more help.
14 Comments
Asyran Abdullah
on 27 Sep 2018
Edited: Asyran Abdullah
on 27 Sep 2018
Yes, correct. How can i change the input "distAllTier1(i,j)" into multi scalar?
I try to make like this, distAllTier1(i,j:2) but it doest not work.
The complete code is almost 500++ line, this problem came out because i change the matrix as my previous question in --> here
I attach again the related code:
numPCHTier1 = 0;
for i = 1:1:numNodes
if node(i).pch == 1 && node(i).tier == 1
numPCHTier1 = numPCHTier1 + 1;
indPCHTier1(1,numPCHTier1) = i;
end
end
distAllTier1 = zeros(numNodes,numPCHTier1);
for i = 1:1:numNodes
for j = indPCHTier1
if node(i).tier == 1
if i == j
distAllTier1(i,j) = NaN;
else
distAllTier1(i,j)= sqrt((node(i).x - node(j).x).^2 + (node(i).y - node(j).y).^2);
end
else
distAllTier1(i,j) = NaN;
break;
end
end
end
Try using cells...
distAllTier1 = cell(numNodes,indPCHTier1) ;
count = 0 ;
for i = 1:1:numNodes
for j = indPCHTier1
count = count+1 ;
if node(i).tier == 1
if i == j
distAllTier1{i,j} = NaN;
else
distAllTier1{i,j}= sqrt((node(i).x - node(j).x).^2 + (node(i).y - node(j).y).^2);
end
else
distAllTier1{i,j} = NaN;
break;
end
end
end
Thanks @KSSV for your help, when change to cell it appear error like this:
Error using cell
Size inputs must be scalar.
KSSV
on 27 Sep 2018
There was a typo error..now modified...check it now.
Asyran Abdullah
on 27 Sep 2018
Edited: Asyran Abdullah
on 27 Sep 2018
WOW! you are so fantastic! Thanks it works!
However, i change the line to this:
distAllTier1 = cell(numNodes,numPCHTier1) ;
Thanks
I'm sorry sir, regarding for changing into cell, i got an issues here :
distAllTier1(distAllTier1 == 0) = NaN;
Above line appear some error:
Undefined operator '==' for input arguments of type 'cell'.
I try to change ( ) to { }, its still same.
Thanks
You cannot use cells like that. See this:
k{1} = [1 0] ;
idx = k{1}==0 ;
k{1}(idx) = NaN
Then i change k into distAllTier1. Thanks sir!
Sir, one more sir.
for i = 1:1:noTier1
if node(i).pch ~= 1
[minVal,index] = min(distAllTier1(i,:));
node(i).head = index;
end
end
It contain error:
Undefined function 'min' for input arguments of type 'cell'.
Error in Untitled10 (line 267)
[minVal,index] = min(distAllTier1(i,:));
The cell cannot read min value. Again i try to use { } but Matlab said,
Error using min
Too many input arguments.
I hope u can help me for last time. Thanks Dr.
min([distAllTier1{i,:}]);
I try to find the solution, but could not solve it. After do min([distAllTier1{i,:}]); it appear this error:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in Untitled10 (line 267)
[minVal,index] = min([distAllTier1{i,:} ] );
[distAllTier1{i,:}]
Try the above line alone.....this should give you a vector.
Thanks sir, i appreciate your time and help .
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)