arraysmatlabif-statementsearch# Other ways to efficiently search within an array

I started with this code except the comments:

```
mol1...;mol2...;
r1 = size(e, 1);%number of candidates for aligned amino acid
r0 = size(e0,1);%number of candidates for reference amino acid
for i = 1 : r1
%if e(i, 1) > 4
for j = 1 : r0
%if e0(j, 1) > 4
if e(i, 1) == e0(j, 1)
eI(i, j) = e(i, 1);%number of atoms matched
eT(i, j) = abs((e(i, 2) - e0(j, 2)) / e0(j, 2) * 100);
end
%end
end
%end
end
```

mol1 and mol2 are the combinations of selecting atoms and the total number: ex for a 3 atom molecule (1, 1,0,0) (1, 0,1,0) ... (3, 1,1,1). e and e0 are some numbers regarding geometry.

When I get to more atoms the size of the array can be 200 000. I thought that it wouldn't hurt to lose combinations of less than 5 atoms, but the code did not run faster. So the problem is with the ifs. Next I tried to delete combinations <5, keep the indexes and rebuild the initial array afterwards:

```
e (:, 7) = find(e (:, 1));
e0(:, 7) = find(e0(:, 1));
e (e (:, 4) < 5, :) = [];
e0(e0(:, 4) < 5, :) = [];
...
```

This halved the time. I tic-toc-ed the code of 500 lines and the problem is here. It would take 2 years for 300 molecules (that I have chosen until now) and I would like to ad some more (20000).

So what other ways of scraping atoms in my array can you guys think of? Maybe I should decide for each size of molecules (15 atoms can scrap results of 5 atoms; 8-4). If changing the precision would reduce this time, how should I do it?

Version 2: cannot submit - webpage won't allow - see comments

Version 3 (50x faster than v2):

```
e(:,7)=find(e (:, 1));
e0(:,7)=find(e0 (:, 1));
[val,ia,ib]=intersect(e(:, 1), e0(:, 1));
for i = 1 : size(ia)
for j = 1 : size(ib)
eI(e(ia(i), 7), e0(ib(j), 7)) = e(ia(i), 1);
eT(e(ia(i), 7), e0(ib(j), 7)) = abs((e(ia(i), 2) - e0(ib(j), )) / e0(ib(j), 2) * 100);
end
end
```

Solution

Here is a vectorized form that computes the result without any for loop:

```
e_eq_e0 = e(:, 1) == e0(:, 1).';
eI = e_eq_e0 .* e(:, 1);
eT = e_eq_e0 .* abs((e(:, 2) ./ e0(:, 2).' - 1) * 100);
```

However the main problem in your code is that you don't pre-allocate the matrices `eI`

and `eT`

before using them:

```
eI = zeros(r1, r0);
eT = zeros(r1, r0);
for i = 1 : r1
....
```

- Function argument list doesn't match function template (function template is made for passing stack allocated array)
- Why is this Array.prototype.reduce method not working
- Find Repeat Sequence in an Array
- Removing duplicate values from a PowerShell array
- How to make arrays/json appear in the correct way
- Python: How to get values of an array at certain index positions?
- How to remove Object from array using mongoose
- How can I count the length of char in a char array without using any loop?
- How to transform nested JSON to CSV with Flatten?
- Fetch Max & Min number in a VBA collection
- ValueError: shape mismatch: objects cannot be broadcast to a single shape
- Group values from a 3d array into an associative 2d array and sum values with a shared path
- Group data from a 2d array by one column then conditionally populate keys within the group and count the number of subarray key occurrences
- How to extract matching strings from array and create new array
- Split flat array into groups/chunks of N elements then sum each group
- Dart List<dynamic> Can't Convert In To List<MyModel>
- C - Return a modified array of integers using pointers
- How to find number of occurrences based on input value
- How to flatten nested array struct in Bigquery sql?
- Using a string to define Numpy array slice
- javascript - match string against the array of regular expressions
- C Problem passing pointer to struct in an array
- Select a certain element from a .map array in react
- How to create object-based form-data when there are multiple, equally named form-elements?
- Numpy Array of Numpy Arrays. Reshaping not working as expected
- When I print the array in unorder list, it will duplicate print on click event
- Map function not returning all the arrays
- How to count Matching values in Array of Javascript
- Kotlin: Create and refer true Java arrays (for JNA)
- subsequent IF statement: shorter ways to assign values to parameters based on another variable value