Can some one help me to wrap around the edge of array?
15 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
For example, i have an array of character
'hiol
hele
abec
mnvn'
and I have a wordSearch = 'love'
Can you one help me to find the wordSearch in the column 3 of the array that can warp around the edge? As everyone can see in this one, that the column 3 that match with the wordSearch start from the column3 row 2, then go up to column3 row1, then wrap around back to column3 row4 then up to column3 row3. Thank you very much.
3 Commenti
Cedric
il 9 Ott 2017
Modificato: Cedric
il 9 Ott 2017
What do you mean by compare? Do you have to find the position of e.g. the 'L' (?), the direction (?), or just whether it is present or not?
The approach really depends on what you are learning (I suppose that it is a homework).
You could get it by finding where the 1 is located in:
>> conv2( [A;A]+0, 1/'love'.', 'same' )
ans =
0.8814 0.8898 0.9407 0.9153
0.8814 0.8559 0.9153 0.8559
0.8220 0.8305 0.8559 0.8390
0.9237 0.9322 1.0000 0.9322
0.8814 0.8898 0.9407 0.9153
0.8814 0.8559 0.9153 0.8559
0.8220 0.8305 0.8559 0.8390
0.9237 0.9322 1.0000 0.9322
but I suppose that you are practicing loops more than convolutions. If so, then maybe you can write two nested loops, one that scans the columns and the other rows 4 to 8 of [A;A] in search of a pattern..
And you may even be able to skip the concatenation by the way. Say
>> rowIds = 1 - (0:3)
rowIds =
1 0 -1 -2
where 1 is the row ID of the row that contains the 'L' and you are building a vector of row IDs that target where all the letters are. Given the wrapping, this vector should be:
1 4 3 2
Maybe you can find a way to remap all elements of rowIds that are below 1 to some number plus them, which would remap 0, -1, -2 to 4, 3, 2. If you are able to do this, you can work with A directly and scan rows 1 to 4.
...
...
rowIds = rowId - (0:3) ;
rowIds(..) = ... the remap ...
if ...
end
...
...
Risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!