http://www.mathworks.com/matlabcentral/newsreader/view_thread/241865
MATLAB Central Newsreader  Trouble Shooting a Function
Feed for thread: Trouble Shooting a Function
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sun, 04 Jan 2009 02:26:01 +0000
Trouble Shooting a Function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241865#619771
Garrett
Hey everyone,<br>
<br>
I'm working on a function that sorts information out of multiple arrays and compares them together. I have a question about why one of my lines of code isn't working. Its line 21 of the script. I want it to give me the row and column of the original array 'y' but, it is giving me the row and column of the variable I created 'chon'. How do I get the row and column of the 'y' at the value position 'chon'?<br>
<br>
Here is the function script<br>
<br>
1:function loca = location (x,y)<br>
2:% A function that finds the location of items within a given range in an array. The 3:output array is<br>
4:% displayed so that the first column is the row of the inputed array the<br>
5:% second column is the column of the inputed array and the third column is<br>
6:% the value of the item from the inputed array.<br>
7:high = input('What is the maximum threshold of intensity emission for the Molecule');<br>
8:low = input('What is the maximum background intensity threshold');<br>
9:range = find(x>low & x<high);<br>
10:[row, column] = find(x>low & x<high);<br>
11:locationx = [row, column, x(range)];<br>
12:disp('The items in array One within the given range are:')<br>
13:disp(locationx)<br>
14:range = find(y>low & y<high);<br>
15:[row, column] = find(y>low & y<high);<br>
16:locationy = [row, column, y(range)];<br>
17:disp('The items in array Two within the given range are:')<br>
18:disp(locationy)<br>
19:diff = xy;<br>
20:chon = find(diff<0 & y>low & y<high);<br>
21:[row,column] = find(y(chon));<br>
22:locationon = [row,column,y(chon)]<br>
<br>
Any ideas would be appreciated.<br>
<br>
Thanks,<br>
<br>
Garrett Banton

Sun, 04 Jan 2009 04:16:02 +0000
Re: Trouble Shooting a Function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241865#619779
Roger Stafford
"Garrett " <bantong@onid.orst.edu> wrote in message <gjp6np$5rq$1@fred.mathworks.com>...<br>
> .....<br>
> I'm working on a function that sorts information out of multiple arrays and compares them together. I have a question about why one of my lines of code isn't working. Its line 21 of the script. I want it to give me the row and column of the original array 'y' but, it is giving me the row and column of the variable I created 'chon'. How do I get the row and column of the 'y' at the value position 'chon'?<br>
> <br>
> Here is the function script<br>
> ......<br>
> 20:chon = find(diff<0 & y>low & y<high);<br>
> 21:[row,column] = find(y(chon));<br>
> .......<br>
<br>
You can solve simple problems like this by yourself with a few small experiments, Garrett. Imagine yourself trying the following.<br>
<br>
y = [1,0,1];<br>
c = find(y>=0);<br>
f = find(y(c));<br>
<br>
The single y that is both greater than or equal to 0 and at the same time nonzero is the 1 value whose position in y is 3rd. You are apparently hoping that f would be a 3 because that is its location in y and instead it comes out a 2.<br>
<br>
You should thereupon proceed to investigate further by displaying some of the intermediate results. We have:<br>
<br>
c = [2,3]<br>
<br>
That is correct. The 2nd and 3rd elements of y are the ones that are >= 0. Then display y(c)<br>
<br>
y(c) = y([2,3]) = [0,1]<br>
<br>
That is correct too. These are indeed the 2nd and 3rd elements of y. Now try this<br>
<br>
f = find(y(c))<br>
<br>
which is the same thing as doing<br>
<br>
f = find([0,1])<br>
<br>
and you get f = 2. Why? The light dawns! It has to be that way because the nonzero element of [0,1] is its 2nd one. It doesn't even have three elements so a 'find' value of 3 would be impossible.<br>
<br>
Your difficulty here has been that you expected that somehow the 'find' function in find(y(c)) would be aware of the nature of y, but that isn't true. It only knows what it sees in y(c) = [0,1] which is a different array and the addresses it comes up with differ accordingly. The y element 1 that was 3rd is now 2nd, and this second 'find' has no way of knowing that it was formerly the 3rd element of y. It can only use what it is given as an argument. It doesn't see y. It sees only y(c) where the y elements have different locations.<br>
<br>
I will let you figure out how to correct the code in accordance with your expectations.<br>
<br>
Roger Stafford