Matlab, finding common values in two array - arrays

I want to find common values in two arrays(For example if A=[1 2 3 4 5 6] and B=[9 8 7 6 3 1 2] the result is ans=[1 2 3 6]).Is there any method without using loop?
Thanks

use intersect(A,B) to get the answer.
Another option is to use ismember, for example A(ismember(A,B)).

Related

Unique array algorithm with incrementing duplicates

Need assistance in designing an algorithm to handle this need. Starting input would be an array that is already known to contain unique integers. Not necessarily ordered or sequential, could be positive or negative. Need to push a new integer to the array and keep items unique by incrementing the conflicting items. The order that they appear in should be preserved though. For example:
Start with array [1 5 3 4 6] and insert 3.
The sequence should be:
[1 5 3 4 6 3] - push 3
[1 5 4 4 6 3] - increment 3 to 4
[1 5 4 5 6 3] - increment 4 to 5
[1 6 4 5 6 3] - increment 5 to 6
[1 6 4 5 7 3] - increment 6 to 7
The input will not be very long, so efficiency is not a major concern, but should be manageable.
If you don't create some other data structure to work in tandem with your unsorted main list, then your append will run in O(n^2), since worst case it has to find and increment every element in the list. Since you said the list is not long and efficiency should not be a problem, I assume this is ok.
Since you didn't specify a language, I can give you some pseudo-code for the basic algorithm you can use.
def incrementHelper(list, e):
if (list.contains(e)):
incrementHelper(list, e+1)
list.set(list.indexOf(e), e+1)
def appendAndIncrement(list, e)
incrementHelper(list, e)
return list.append(e)
Edit: You are correct, that other solution wouldn't work - my bad. This one uses a recursive solution and should avoid the problem you mentioned.

Initializing multi dimensional arrays in lua/torch

I have gone through a few tutorials about "arrays" in lua/torch and all I see is the word tensors. What exactly are they? How can i initialize a 2d "tensor"? I tried torch.Tensor{1,2,3} which gave me
1
2
3
and torch.Tensor{1,2,3;4,5,6} like in octave/MATLAB but this gave me another column vector
1
2
3
4
5
6
How can I get a 2d "tensor" like the following?
1 2 3
4 5 6
Also, I'm using torch to execute my lua files. Is there no other notation for arrays other than "tensors" i.e. is there no other way we can use to represent a matrix in torch?
torch.Tensor{{1, 2, 3}, {4, 5, 6}}
Have a look at this (or that) for more details on tensors.

Difference between two arrays and create a special array [duplicate]

This question already has answers here:
Insert value at a specific spot in matlab vector or matrix
(2 answers)
Closed 7 years ago.
I think it will be better to give an example to describe my problem clearly.
Input:
a=[1 2 3 4 5 6 7 8 9 10]
b=[2 5 8]
Output:
c=[1 0 2 3 0 4 5 0 6 7]
I am trying to solve this problem using MATLAB, and looking for any efficient way or MATLAB in build function to solve this problem.
I also tried to solve it, but it requires one for loop and few if-else statements inside it.
Okay. I do not know which will work better, but I got an idea and thought to share it.
a = 1:10;
b = [2 5 8];
c = ones(1,10);
c(b) = zeros(size(b));
c(c~=0) = 1:(length(a)-length(b));

matlab find specific VALUES in an array

How to find out all array element indices equal to several values (>2)
For example, I have an array a=[1 2 3 4 5 5 4 3 2 2 2 1], I want to know the indices of all elements equal to b=[2 5]
Remember, I cannot use style like a==b(1) | a==b(2) because number of elements in b is arbitrary.
Do I have to use a for loop to do this?
You can use ismember (as Daniel said just seconds before I hit enter...) ;-)
a=[1 2 3 4 5 5 4 3 2 2 2 1];
b=[2 5];
c=find(ismember(a,b))
Output:
c =
2 5 6 9 10 11
If you want to do it more manually, you can use bsxfun:
c = find(any(bsxfun(#eq, a(:).', b(:)), 1));

Find elements in array those have come two times in array

Given A = [3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 8]
Output B = [3 4 5 6 8]
Is there a Matlab function or command to get this result? I am new to Matlab. Just now I am doing it going through for each element and keeping a counter for it. I have very big array so this is taking too much time.
Use a combination of unique and histc:
uA = unique(A); %// find unique values
B = uA(histc(A, uA)>=2); %// select those that appear at least twice
The above code gives the values that appear at least twice. If you want values that appear exactly twice, replace >= by ==.

Resources