필터 지우기
필터 지우기

Find index of an array that corresponds to a given value

조회 수: 3 (최근 30일)
Michael
Michael 2018년 8월 11일
댓글: Star Strider 2018년 8월 12일
Given an array of known point values (Xpoints), which is a 1 x 11 array I'm trying to search and generate a list of index locations that correspond to when the X_ValueTrans (1 x 501) equals the individual elements in the Xpoints array. The following code is able to located the first three but on the fourth time through the loop I get the following error. Below the error message I have posted the code sample I am using. In addition, I have changed the loop1 step thinking it couldn't find the fourth value. However, I changed the step size of the for loop to 2 and it still only finds the first three terms. I must be missing something
--------- Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-0.
Error in PlotCurve (line 34) XpIndex (1, loop1) = [col]
-------------Code Below-----
XpIndex = zeros(1, length(Xpoints)); X_ValueTran = X_Value'
for loop1 =1:1: NumberLines+1
[row,col] = find(X_ValueTran == Xpoints(1,loop1))
XpIndex (1, loop1) = [col]
end

채택된 답변

Star Strider
Star Strider 2018년 8월 11일
I can only guess as to what the exact problem may be without seeing your data. It could be that what you believe to be the 4th element is not exactly equal. (See Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? for a discussion of a possible reason.)
You may need to incorporate a tolerance value to find elements that are close but not exactly equal, using the ismembertol (link) function. For an illustration, see the documentation section on Group Scattered Data Using a Tolerance (link).
  댓글 수: 2
Michael
Michael 2018년 8월 12일
I think this may be the issue. The original data was an array of number for simplicity 0 to 0.5 in 0.001 and the match array (Xpoints = 0.1, 0.15, 0. 24 etc..)
this caught most of the points but oddly missed a few [ida,idb] = ismember(X_ValueTrans,Xpoints)
but after changing to the ismember to ismembertol see below it found all the values and I was able to use an if statement to get the array index values.
Thanks for your help.
[ida,idb] = ismembertol(X_ValueTran,Xpoints, 0.00001);
temp =1 for loop1 =1:1: length(idb)
if [idb(loop1)] > 0
XpIndex (1, temp) = loop1
temp = temp+1
end
end
Star Strider
Star Strider 2018년 8월 12일
As always, my pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by