How to compare two vectors element by element? [duplicate] - matlab

This question already has answers here:
How do I compare all elements of two arrays?
(3 answers)
Closed 6 years ago.
I have two matrices: A=[1,2,3,4,5] and B=[1,2,3,4,6]. I need to compare elements of those matrix, and as a result I need to have binary matrix
Result=[1,1,1,1,0], that means if A(i)==B(i) then Result(i)=1 else Result(i)=0.
I have tried with:
if (isequal (A,B))
Result=1
else
Result=0
end
I have tried: Result=sign(A,B)
I hope that you could help me please?

A = [1,2,3,4,5]
B = [1,2,3,4,6]
Result = A == B

Related

How do I create a struct? [duplicate]

This question already has answers here:
How to initialize an array of structs in MATLAB?
(7 answers)
Closed 2 years ago.
How do I create an array in struct to keep track of all my_exp?
My code
my_exp=struct(x, y);
for i = 1:32
my_exp.x= i;
my_exp.y= i*i;
end
my_exp
Make sure you initialize your structure properly.
You want to do:
my_exp=struct('x',[],'y',[])
for i = 1:32
my_exp.x= [my_exp.x i];
my_exp.y= [my_exp.x i*i];
end
my_exp

Swift 3: Remove one character from set [duplicate]

This question already has answers here:
Removing duplicate elements from an array in Swift
(49 answers)
Closed 5 years ago.
Say I have a set [A, B, B, C, D], how would I remove just the first B?
If charToDelete = B and I do this:
SlidingWin.remove(charToDelete)
won't it remove all of the B characters?
In Swift lingo, what you have is an array (not a set). If you want to remove the first "B" from an array, you can do this:
if let index = array.index(of:"B")
{ array.remove(at:index) }
[EDIT] example of a functional approach for anagrams:
let set1 = "cabb"
let set2 = "cbabeijbbacbkiie"
let anagrams = zip(set2.indices,set2.indices.dropFirst(set1.count-1))
.map{set2[$0...$1]}
.filter{$0.sorted() == set1.sorted()}

Remove specific integers from matrix [duplicate]

This question already has answers here:
Exclude elements from array [duplicate]
(3 answers)
Closed 6 years ago.
I have a matrix <1x1000> containing integers. It contain the value 150 a couple of times and I want to remove that value completely. Any ideas how to?
Help is much appreciated!
If you want to remove all elements that equal 150 then
M = M(M ~= 150)
If you want to remove all elements belonging to a list of undesired numbers then
list = [150, 230, 420]
M = M(~ismember(M, list))
Same but different expression
M(M==150)=[];
list = [150,230,420];
M(ismember(M,list))=[];
When you type A(index)=[], it delete A(index). For example,
A = [1,2,3];
A(2) = [];
Then
A = [1,3]

How to quickly check if an Integer lies within a given range [duplicate]

This question already has answers here:
Can I use the range operator with if statement in Swift?
(6 answers)
Closed 6 years ago.
I have an integer x and I want to check if it lies between a given boundary / within a given range.
The straightforward approach would be
let contains = x > lowerBounds && x < higherBounds
Is there a more swifty approach to this?
You can create a range and check if it contains x:
let contains = (lowerBounds...upperBounds).contains(x)
e.g.:
let successful = (200..<300).contains(httpStatusCode)
Or you can use the pattern matching operator:
let contains = lowerBounds...uppperBounds ~= x

Scala 2 dimensional Array and Arrays [duplicate]

This question already has answers here:
How to create and use a multi-dimensional array in Scala?
(3 answers)
Closed 8 years ago.
I am trying to write my Java ode from in Scala and I think I need some help.
my problem:
Java:
public static int[][] ScoreMatrix = new int[5][20];
Scala:
var ScoreMatrix: Array[Array[Int]] = new Array[Array[Int]](5, 20)
It's not working, don't know why?
Error "too many arguments for constructor Array(_length:int)Array[Array[Int]]"
For initializing 5*20 2D int array you can use:
var ScoreMatrix: Array[Array[Int]] = Array.ofDim[Int](5, 20)
Your code doesn't work because the Array constructor has only one argument, which is the array length.
Consider also
Array.tabulate(5,20)( (x,y) => 1)
which instantiates a 5 by 20 array with Int: 1 (in general a function of x and y).