Assignment has more non-singleton rhs dimensions than non-singleton subscripts

I am trying to execute a loop and getting this error how can i get out of it
% distanceOfAllNearestTargets = zeros(numberOfNearestNeighbors, 1);
locationNumberOfAllNearestTargets = zeros(numberOfNearestNeighbors, 1);
for ii = 1 : 1 : numberOfNearestNeighbors
locationNumberOfAllNearestTargets(ii, 1) = find(distanceOfAllTargetsFromCurrentPosition == min(distanceOfAllTargetsFromCurrentPosition));
distanceOfAllNearestTargets(ii, 1) = distanceOfAllTargetsFromCurrentPosition(locationNumberOfAllNearestTargets(ii));
distanceOfAllTargetsFromCurrentPosition(locationNumberOfAllNearestTargets(ii, 1)) = max(distanceOfAllTargetsFromCurrentPosition);
end

8 件のコメント

madhan ravi
madhan ravi 2018 年 10 月 31 日
numberOfNearestNeighbors?
muhammad ahmad
muhammad ahmad 2018 年 10 月 31 日
10
Walter Roberson
Walter Roberson 2018 年 10 月 31 日
Can you guarantee that there is only exactly one value that is the minimum distance? Probably not.
muhammad ahmad
muhammad ahmad 2018 年 10 月 31 日
yeah not
Rik
Rik 2018 年 10 月 31 日
A non-related note: I want to compliment you on the use of descriptive variables.
madhan ravi
madhan ravi 2018 年 10 月 31 日
Upload all the necessary details , saves time!
muhammad ahmad
muhammad ahmad 2018 年 10 月 31 日
mean? discriptive variable?
muhammad ahmad
muhammad ahmad 2018 年 10 月 31 日
@madhan Ravi, I have a quite complex code it is just a function of that code.in which I need to generate an array of distances equal to a number of nearest target

サインインしてコメントする。

回答 (1 件)

Rik
Rik 2018 年 10 月 31 日
編集済み: Rik 2018 年 10 月 31 日
find is not guaranteed to result 1 value. It might return a vector, or an empty array. If you are certain there will always be 1 result or more, you can use the later inputs to find to limit the results it returns to either the first one or the last one. If that doesn't fit your situation, you should store the find result in a temporary variable and use switch (or if) to handle the three cases (so numel(temp_result)==0, >1, and ==1).
locationNumberOfAllNearestTargets(ii, 1) = find(distanceOfAllTargetsFromCurrentPosition == min(distanceOfAllTargetsFromCurrentPosition),1,'first');

6 件のコメント

muhammad ahmad
muhammad ahmad 2018 年 10 月 31 日
did not exactly got your idea
Rik
Rik 2018 年 10 月 31 日
find returns a vector, which you try to store in a single position. That doesn't fit, so you have to pick 1 result from that vector. By using my suggested line of code as a replacement, find only returns the index to the first result it finds.
Try it out with a smaller example:
A=[0 0 1 1 0 1 0];
find(A)
find(A,1,'first')
find(A,1,'last')
muhammad ahmad
muhammad ahmad 2018 年 11 月 1 日
it works but how to do it in loop
Walter Roberson
Walter Roberson 2018 年 11 月 1 日
To do it in a loop, put a loop around it ?
We can see that you know how to write loops, so we are not clear as to what the question is?
muhammad ahmad
muhammad ahmad 2018 年 11 月 1 日
yeah but doing this in a loop cause same error after one iteration
Rik
Rik 2018 年 11 月 1 日
Try to make a small example that reproduces your error. You can attach a mat file with the actual data to your comment if you can't figure out a way to generate some example data.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2018 年 10 月 31 日

コメント済み:

Rik
2018 年 11 月 1 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by