How to search and find array in array?

Hello,
I create an array below;
bigArray = rand(1,150);
bigArray(1,15:19) = [1 1 1 1 1]';
bigArray(1,25:29) = [1 1 1 1 1]';
bigArray(1,75:79) = [1 1 1 1 1]';
bigArray(1,105:109) = [1 1 1 1 1]';
bigArray(1,65) = 1;
bigArray(1,5:6) = [1 1]';
I want to find [1 1 1 1 1]' array indexes. But I run the code;
idx = find(ismember(bigArray,[1 1 1 1 1]'))
idx = 1×23
5 6 15 16 17 18 19 25 26 27 28 29 65 75 76 77 78 79 105 106 107 108 109
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I want to see as an output; [15 16 17 18 19 25 26 27 28 29 75 76 77 78 79 105 106 107 108 109]

 Accepted Answer

The ismember function is doing exactly what it should. You need to examine ‘bigArray’ tto understand its output.
Try this —
bigArray = rand(1,150);
bigArray(1,15:19) = [1 1 1 1 1]';
bigArray(1,25:29) = [1 1 1 1 1]';
bigArray(1,75:79) = [1 1 1 1 1]';
bigArray(1,105:109) = [1 1 1 1 1]';
bigArray(1,65) = 1;
bigArray(1,5:6) = [1 1]';
disp(bigArray)
Columns 1 through 18 0.5068 0.9505 0.0516 0.0882 1.0000 1.0000 0.0318 0.3127 0.8880 0.0266 0.9131 0.8241 0.3292 0.0973 1.0000 1.0000 1.0000 1.0000 Columns 19 through 36 1.0000 0.4800 0.7703 0.2106 0.1942 0.6979 1.0000 1.0000 1.0000 1.0000 1.0000 0.4912 0.4754 0.2845 0.7561 0.9840 0.1853 0.1938 Columns 37 through 54 0.4194 0.0772 0.1116 0.3382 0.2339 0.0543 0.3256 0.9436 0.2975 0.6174 0.2227 0.7096 0.5371 0.9033 0.7377 0.5522 0.5055 0.3581 Columns 55 through 72 0.8638 0.2271 0.3195 0.0197 0.4622 0.0299 0.3055 0.4705 0.3989 0.4399 1.0000 0.6735 0.8151 0.1478 0.8166 0.2860 0.6436 0.8295 Columns 73 through 90 0.4801 0.7913 1.0000 1.0000 1.0000 1.0000 1.0000 0.8977 0.8132 0.6884 0.4697 0.2341 0.1666 0.4997 0.6061 0.5467 0.4436 0.5024 Columns 91 through 108 0.8998 0.2705 0.0215 0.1483 0.4976 0.9649 0.5099 0.9422 0.8145 0.3519 0.6390 0.0093 0.5664 0.9736 1.0000 1.0000 1.0000 1.0000 Columns 109 through 126 1.0000 0.7624 0.4387 0.6305 0.1326 0.6353 0.2669 0.3236 0.1274 0.4486 0.2744 0.5949 0.0264 0.7638 0.9727 0.1876 0.1710 0.6894 Columns 127 through 144 0.3853 0.9315 0.9158 0.8965 0.2594 0.0620 0.3116 0.6300 0.4641 0.4008 0.7085 0.1320 0.1949 0.4087 0.7125 0.2770 0.0934 0.5674 Columns 145 through 150 0.4633 0.3660 0.7867 0.9442 0.3935 0.3620
Lv = ismember(bigArray,[1 1 1 1 1]')
Lv = 1x150 logical array
Columns 1 through 45 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 46 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 135 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 136 through 150 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
idx = find(Lv)
idx = 1×23
5 6 15 16 17 18 19 25 26 27 28 29 65 75 76 77 78 79 105 106 107 108 109
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.

4 Comments

Hilal
Hilal on 13 Sep 2024
Edited: Hilal on 13 Sep 2024
Thank you but I do not want to see 5 6 and 65 indexes. I just need 5 times repeated value indexes as an output. Also it should be faster than for loop.
To search for a specific pattern, use the strfind functiion. (It seems counterintuitive to use that here, however it works, after a fashion).
bigArray = rand(1,150);
bigArray(1,15:19) = [1 1 1 1 1]';
bigArray(1,25:29) = [1 1 1 1 1]';
bigArray(1,75:79) = [1 1 1 1 1]';
bigArray(1,105:109) = [1 1 1 1 1]';
bigArray(1,65) = 1;
bigArray(1,5:6) = [1 1]';
disp(bigArray)
Columns 1 through 18 0.2743 0.2407 0.8275 0.0096 1.0000 1.0000 0.8060 0.9308 0.5000 0.8108 0.5384 0.8746 0.4255 0.4682 1.0000 1.0000 1.0000 1.0000 Columns 19 through 36 1.0000 0.8638 0.2319 0.3997 0.2898 0.1223 1.0000 1.0000 1.0000 1.0000 1.0000 0.1297 0.7206 0.1497 0.6789 0.5847 0.5892 0.7993 Columns 37 through 54 0.2800 0.9085 0.7488 0.2514 0.1398 0.6313 0.5854 0.5353 0.4922 0.5730 0.1827 0.1280 0.9387 0.7217 0.2021 0.0497 0.3776 0.1897 Columns 55 through 72 0.2701 0.6038 0.7225 0.8177 0.6791 0.4523 0.2733 0.2405 0.7571 0.2166 1.0000 0.7963 0.6565 0.4824 0.2540 0.5467 0.0953 0.0753 Columns 73 through 90 0.4202 0.5929 1.0000 1.0000 1.0000 1.0000 1.0000 0.4567 0.3059 0.8577 0.0012 0.2719 0.9140 0.5484 0.2816 0.5734 0.8117 0.6676 Columns 91 through 108 0.5733 0.8570 0.0506 0.4219 0.2212 0.6640 0.7136 0.5508 0.5685 0.0510 0.7710 0.4171 0.2690 0.0529 1.0000 1.0000 1.0000 1.0000 Columns 109 through 126 1.0000 0.4731 0.3191 0.4956 0.4133 0.3051 0.1663 0.6878 0.8143 0.6790 0.9396 0.2974 0.8329 0.4112 0.1258 0.7320 0.4892 0.6529 Columns 127 through 144 0.9542 0.6012 0.7828 0.5601 0.3783 0.7228 0.5640 0.1776 0.6977 0.3227 0.6249 0.0279 0.2191 0.9455 0.1715 0.1862 0.7512 0.2973 Columns 145 through 150 0.7227 0.7914 0.1098 0.6703 0.2003 0.1265
pattern = [1 1 1 1 1];
Idx = strfind(bigArray,pattern);
Idx = 1×4
15 25 75 105
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Idx = reshape([Idx(:) Idx(:) + repmat((1:numel(pattern)-1), numel(Idx), 1)].', 1, []);
disp(Idx)
15 16 17 18 19 25 26 27 28 29 75 76 77 78 79 105 106 107 108 109
The strfind function returns only the first index of the pattern, so it is necesary to add:
(1:numel(pattern)-1)
to each element. I did that here by transposing the original ‘Idx’ vector (the Idx(:) step), and summing it with that vector to create a matrix, then contatenating the transposed ‘Idx’ vector to the matrix I created in the first step, and then reshaped that to a row vector to get the desired result. It is not straightforward to get the result you want, however iit is definitely possiible.
The ismember function is quite useful at finding individual instances of the elements of its second argument, however it does not find patterns. There are other functions that operate similarly to strfind (the See Also section of the strfind function documentation page has a list of them), however strfind is a function everyone has.
.
Thank you very much!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 12 Sep 2024

Commented:

on 13 Sep 2024

Community Treasure Hunt

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

Start Hunting!