How do you get the row or column of something you need?

For example, if you had:
abc
def
ghi
Say I wanted the row and column of 'e', how would I do this? I have been looking everywhere for this and I just can't seem to find it. Please help. Thank you!

 Risposta accettata

[rows, columns] = find(yourMatrix == e); % e is an integer.

5 Commenti

Thanks but I am using this for a wordsearch so there are many different instances of the same letter and I need to output the row and column of the first letter. When I use what you answered, I keep getting the same output of row and column equalling 1. Please help if you know the problem.
Shawn, you did something wrong. Let's see your code. Here is correct code for letters (character array):
% Create sample array of characters
letterMatrix = char(randi([65, (65+25)], 5,10))
% Find all locations where the A is:
[rows, columns] = find(letterMatrix == 'A')
Results in command window:
letterMatrix =
EEZONLUYBE
GNWBASAFML
PBCSYZFLBG
CBMRKKWBTM
YFDUZJKTZX
rows =
2
2
columns =
5
7
You can see that it's not just (1,1). We get (2, 5) and (2, 7) which matches up with the locations you see in the displayed matrix.
Shawn Simon
Shawn Simon il 28 Nov 2015
Modificato: Shawn Simon il 28 Nov 2015
I see what I did wrong! Thank you so much!! But I have one more question! If there are multiple instances of A, how do I get find to only output one of them?
You decide which position is the "first" one, and you index rows() and columns() at that same index. But how do you define which is the "first" ?
AB
BD
which 'B' is 'first' ?
The way that I want to implement this, is with a wordsearch. I have a 15x15 2 dimensional array filled with letters. I have created a function that runs checks throughout the array looking for the word a user inputs. With this, I am to return the row and column of the first letter of the word. So it would be like this:
iuwekfasdkuhwqa
kjdfjdjilihvjut
injmatlabiliwtm
ijghaddndldndld
And so on. The user would then use the function I created and input a word, in this case 'matlab'. The function would then output the row and column of 'm' (3rd row; 4th column). If you would like to see my code, I would gladly show you.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by