"Unable to perform assignment because the left and right sides have a different number of elements error" using find command
1 次查看(过去 30 天)
显示 更早的评论
reference=[1 2 3 4 5 6 8 9 10];
target=[1 2 3 4 6 7 8 9 10];
for i=1:9
index(i)=find(reference(i)==target);
end
Here, after i reaches 4, "unable to perform assignment" error occurs because:
find(reference(5)==target) = 0×1 empty double column vector
. How I can create the index array without encountering this error such as:
index=[1 2 3 4 0 0 1 1 1]; %or
index=[1 2 3 4 NaN NaN 1 1 1];
My matlab version is R2019a.
0 个评论
采纳的回答
Walter Roberson
2021-9-23
reference=[1 2 3 4 5 6 8 9 10];
target=[1 2 3 4 6 7 8 9 10 3 8];
index = [];
complete_reference = [];
for i=1:9
thisref = reference(i);
match = find(thisref==target);
if isempty(match)
complete_reference(end+1) = thisref;
index(end+1) = NaN;
else
index = [index, match];
complete_reference = [complete_reference, repmat(thisref, 1, numel(match))];
end
end
complete_reference
index
We must assume that there could be multiple matches between the target and the reference, not just none or exactly 1, so we created an extended reference that repeats the reference number as often as needed to put in all of the matches.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!