Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Trouble Shooting a Function

Subject: Trouble Shooting a Function

From: Garrett

Date: 4 Jan, 2009 02:26:01

Message: 1 of 2

Hey everyone,

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'?

Here is the function script

1:function loca = location (x,y)
2:% A function that finds the location of items within a given range in an array. The 3:output array is
4:% displayed so that the first column is the row of the inputed array the
5:% second column is the column of the inputed array and the third column is
6:% the value of the item from the inputed array.
7:high = input('What is the maximum threshold of intensity emission for the Molecule');
8:low = input('What is the maximum background intensity threshold');
9:range = find(x>low & x<high);
10:[row, column] = find(x>low & x<high);
11:locationx = [row, column, x(range)];
12:disp('The items in array One within the given range are:')
13:disp(locationx)
14:range = find(y>low & y<high);
15:[row, column] = find(y>low & y<high);
16:locationy = [row, column, y(range)];
17:disp('The items in array Two within the given range are:')
18:disp(locationy)
19:diff = x-y;
20:chon = find(diff<0 & y>low & y<high);
21:[row,column] = find(y(chon));
22:locationon = [row,column,y(chon)]

Any ideas would be appreciated.

Thanks,

Garrett Banton

Subject: Trouble Shooting a Function

From: Roger Stafford

Date: 4 Jan, 2009 04:16:02

Message: 2 of 2

"Garrett " <bantong@onid.orst.edu> wrote in message <gjp6np$5rq$1@fred.mathworks.com>...
> .....
> 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'?
>
> Here is the function script
> ......
> 20:chon = find(diff<0 & y>low & y<high);
> 21:[row,column] = find(y(chon));
> .......

  You can solve simple problems like this by yourself with a few small experiments, Garrett. Imagine yourself trying the following.

 y = [-1,0,1];
 c = find(y>=0);
 f = find(y(c));

The single y that is both greater than or equal to 0 and at the same time non-zero 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.

  You should thereupon proceed to investigate further by displaying some of the intermediate results. We have:

 c = [2,3]

That is correct. The 2nd and 3rd elements of y are the ones that are >= 0. Then display y(c)

 y(c) = y([2,3]) = [0,1]

That is correct too. These are indeed the 2nd and 3rd elements of y. Now try this

 f = find(y(c))

which is the same thing as doing

 f = find([0,1])

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.

  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.

  I will let you figure out how to correct the code in accordance with your expectations.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us