# Finding a Value in a vector

Discussion in 'iOS Programming' started by hugolohrer, Nov 14, 2011.

1. ### hugolohrer macrumors newbie

Joined:
Nov 14, 2011
#1
Hi,

I was programming here and I stopped at a thing. I'll try to explain.
I have a vector A = [10, 20, 30, 40], than I want to the computer tell me the directly lower number of the vector for reported 'x'.
like so:
x = 23
ans = 20.
but i also want him to tell me the row and column of this number.
type so
x = 23
so , i = 2 ; j = 1
i =column
j=row

i know how to do this in MATLAB language but,
how can i do this in Objective-C language ??????

Thanks

2. ### ArtOfWarfare macrumors G3

Joined:
Nov 26, 2007
#2
Neither C nor obj-C have official multidimensional arrays.

You might try something like:
Code:
`int A[2][4] = { 10, 20, 30, 40, 50, 60, 70, 80 }`
The above array, though it was called a 2x4, is as far as C is concerned a 1 dimensional array with 8 spaces.

When the user puts in a number it could use a for loop to check each number in the array... and it needs to store which space the number was found in. Then, to convert it back to a two dimensional array, it would look something like...

Code:
```row = location/rowLength
column = location%rowLength```
Do you get the gist of what I'm saying? Googling "C multidimensional array" might help.

3. ### hugolohrer thread starter macrumors newbie

Joined:
Nov 14, 2011
#3
Sorry have explain completely wrong.

i will try to explain right this time ...........sorry

I have a vector A = [10, 20, 30, 40], than I want the computer to tell me the directly lower number of the informed number 'x'.
like so:
x = 23
ans = 20.
but i also want him to tell me the position of this number.
like so
x = 23
so , i = 2
"i" is the position of the number in the vector

i know how to do this in MATLAB language but

how can i do this in Objective-C language ??????

Thanks

4. ### lloyddean macrumors 6502a

Joined:
May 10, 2009
Location:
Des Moines, WA
#4
Sorry but I have to ask what you mean by vector when you're talking of Objective-C?

Example code would help, me at least, know for sure how best to guide you to an answer.

5. ### Sykte macrumors regular

Joined:
Aug 26, 2010
#5
I think he's talking about vector space and doing vector addition and multiplication.

6. ### hugolohrer thread starter macrumors newbie

Joined:
Nov 14, 2011
#6
sorry....again hehehe

this "vector" is like an array.

i will post here an example of my code for matlab. this code did solve my problem.

So a have the following array:

A=[10;20;30;40;50]

t=input('write the number =')

[K,L]=find(A>t,1,'first')

----------------------

So if the user write ->23

MATLAB tells me this

K=3

L=1

But i just need the "K" value.

hope this help

thanks

7. ### lloyddean macrumors 6502a

Joined:
May 10, 2009
Location:
Des Moines, WA
#7
In other words you have MATLAB code, no Objective-C code and want us to provide you with the equivalent code suitable for use with Objective-C.

Does that sum it up about right?

8. ### ArtOfWarfare macrumors G3

Joined:
Nov 26, 2007
#8
We're not just going to tell you the answer. We're here to help you find the answer for yourself, not do it all for you.

We need to know what you know about obj-c. What have you tried so far?

9. ### lloyddean macrumors 6502a

Joined:
May 10, 2009
Location:
Des Moines, WA
#9
Oh I don't know about that. I'm known for doing exactly that depending upon the apparent frustrations of the poster!

10. ### jiminaus macrumors 65816

Joined:
Dec 16, 2010
Location:
Sydney
#10
I've done it too. Check out the link in this post. The attitude of the OP of that thread is a great example of why it's not a good idea.

11. ### lloyddean macrumors 6502a

Joined:
May 10, 2009
Location:
Des Moines, WA
#11
I feel we're both saying the same things from opposite ends. The difference for me that there are some people that need a few working whole examples to study in order to learn.

Probe and determine!

12. ### hugolohrer thread starter macrumors newbie

Joined:
Nov 14, 2011
#12
Sorry ArtOfWarfare, but you are wrong,

my array is 45x1 (if you understand what's that mean) and my code goes far far far far beyond....
so this is just a little bit of my entire code.

----------

exactly

I asked for help, many many people but they just couldn't answer me correctly.

thank you

13. Nov 14, 2011
Last edited by a moderator: Nov 14, 2011

### hugolohrer thread starter macrumors newbie

Joined:
Nov 14, 2011
#13
for those who (whoo are you who who, who who------reference to the music :Who are You ;by The Who) read these post and want the correct answere:

Code:
```int main()
{
int vetor1[] = {10, 20, 30, 40};
int cont = 0, marc = 0, i = 0;
int value1;
marc = 20.5 - 10;
while(i < 4){
if(marc > abs(vetor1[i] - 20.5)){
marc = abs(vetor1[i] - 20.5);
value1 = vetor1[i];
cont = i;
}
i++;
}
printf("numberdirectlower =  %i\n", value1);
printf("position= %i\n", (cont+1));
return 0;
}```

i did modify a little bit from the one that i'm going to use.
so i don't know if this one is going to run...

sorry.

bye

14. ### lloyddean macrumors 6502a

Joined:
May 10, 2009
Location:
Des Moines, WA
#14
Without a better understanding of exactlly what you're wish to accomplish it really is hard to know where to start helping.

As Objective-C is basically a super set of C you may want to stick with C syntax.

15. ### North Bronson macrumors 6502

Joined:
Oct 31, 2007
Location:
San José
#15
Before you become too comfortable doing vector-work with for-loops, read through the vForce framework. If you are doing math on every item in a vector, the vForce functions will probably be a lot faster than anything that you or I can write with for-loops (and would probably even smoke matlab).