How do I create a struct? [duplicate] - matlab

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

Related

Rewrite C-style for loop in Swift 3 [duplicate]

This question already has answers here:
For-In Loops multiple conditions
(5 answers)
Closed 5 years ago.
I have a C-style for loop that's no longer supported in Swift 3. It looks something like this:
for (var x = 0; x < foo.length && x < bar.length; x++) {}
What's the equivalent of this that's available now in Swift 3?
if you only need to check the two limits, how about this
for x in 0..<min(foo.length, bar.length)
{
// do stuff
}

Get Distinct Members of an Array by property [duplicate]

This question already has answers here:
Removing Duplicates From Array of Custom Objects Swift
(4 answers)
Remove objects with duplicate properties from Swift array
(16 answers)
Closed 5 years ago.
I have an array
Contract object contains:
String:id
String:value
Array is:
contract1 = Contract.new()
contract1.id = 2
contract1.value = "Apple"
contract2 = Contract.new()
contract2.id = 2
contract2.value = "Pen"
contract3 = Contract.new()
contract3.id = 1
contract3.value = "Pineapple"
array = [Contract1, Contract2, Contract3]
I would to find out the list of contracts whose IDs are distinct.
I want to have a solution that doesn't make me change the implementation of my object (overriding the isEqual method etc) since I will be using it for more than one object through out my code.
Desired result:
[contract1, contract3] or [contract2, contract3]
Ideally, an extension with additionally a method to only return the values that are being made distinct:
Desired result: [2, 1]
I tried a couple of approaches from similar questions but either the answers are outdated or doesn't fit my need.

How to append string inside for loop [duplicate]

This question already has answers here:
How do I convert a Swift Array to a String?
(25 answers)
Closed 6 years ago.
I have user array and I want concat values with ','
for s in userArray {
}
I want the result be A,B,C,D
How could I do this in Swift 3?
Use the array joined(separator:) function
let joined = stringArray.joined(separator: ", ")

Faster way to create an array of numbers spanning a range in Swift [duplicate]

This question already has answers here:
Is there a way to instantly generate an array filled with a range of values in Swift?
(4 answers)
Closed 6 years ago.
Is there a shorter way to create an array of numbers spanning a range in swift?
Right now, I'm using this:
var arrayOfInts = [UInt32]()
for number in 1...100 {
arrayOfInts.append(number)
}
Is there a one-line way of doing it?
var arrayOfInts = Array(1...100)
Playground Output
Is this short enough?
let array = Array(1...100)
Try like this
var z = [Int](1...100)
print(z)
DEMO

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

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