How can I compare a boolean to an array? - Swift - swift

I have this:
if sender.tag == 1 || sender.tag == 2 || sender.tag == 3 || sender.tag == 4 || sender.tag == 5 || sender.tag == 6 || sender.tag == 7 || sender.tag == 8 || sender.tag == 9 || sender.tag == 10 {
Is there anyway to minimize this some similar to this:
if sender.tag == [1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10] {

How about
if (1...10).contains(sender.tag)
?

You could do something like:
if 1 <= sender.tag && sender.tag <= 10 {
This checks if sender.tag is between 1 and 10 inclusive.

You can use a set or range:
let tagsSet: Set = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
if tagsSet.contains(sender.tag)

Related

matlab loops for converting unit

while strcmp(unit, "oz") == 0 && strcmp(unit, "lb") == 0 && strcmp(unit, "ton") == 0 && strcmp(unit, "in") == 0 && strcmp(unit, "ft") == 0 && strcmp(unit, "mi") == 0 && strcmp(unit, "F") == 0 && strcmp(unit, "g") == 0 && strcmp(unit, "kg") == 0 && strcmp(unit, "tonne") == 0 && strcmp(unit, "cm") == 0 && strcmp(unit, "m") == 0 && strcmp(unit, "km") == 0 && strcmp(unit, "C") == 0
if strcmp(unit_type, "mass") == 1
while contains(unit, "oz") == 0 && strcmp(unit, "lb") == 0 && contains(unit, "ton") == 0 && contains(unit, "g") == 0 && strcmp(unit, "kg") == 0 && contains(unit, "tonne") == 0
unit = input("Enter the unit: ","s");
end
elseif strcmp(unit_type, "temperature") == 1
while contains(unit, "F") == 0 && contains(unit, "C") == 0
unit = input("Enter the unit: ","s");
end
elseif strcmp(unit_type, "length") == 1
while contains(unit, "in") == 0 && contains(unit, "ft") == 0 && contains(unit, "mi") == 0 && contains(unit, "cm") == 0 && contains(unit, "m") == 0 && contains(unit, "km") == 0
unit = input("Enter the unit: ","s");
end
end
disp('Error: Invalid input. Please enter a valid unit.');
unit = input("Enter the unit: ","s");
end
up there is the piece of the scrip that i wrote for my converting unit program but the thing is I wanted to loop it for when the user wrongly input. For instance, when the user choose mass and input the unit of the value is km it should pop the error and request to input again. Could you guys help me with this? it still runs the rest of the code since the While condition is what im still working on it
What did you try and what were you expecting? for this section im still thinkign of another solution

Recursive function with loops inside

I am trying to generalize loops within a recursive function...
The basic double loop is the following function:
func multiLoops(start ix:Int, upTo n:Int) {
for i in ix...n {
for j in i+1 ... n+1 {
print("it is \(i) \(j)")
}
}
}
multiLoops(start: 3, upTo: 6)
With the following result:
it is 3 4
it is 3 5
it is 3 6
it is 3 7
it is 4 5
it is 4 6
it is 4 7
it is 5 6
it is 5 7
it is 6 7
My tentative to do the same with a recursive function is the following:
func multiLoopsRecursive(start ix:Int, upTo n:Int, loopNumber:Int){
var loopNumber = loopNumber
var previous_i = ix
func loop(start ix:Int, upTo n:Int) {
for i in ix + 1...n {
print("it is \(previous_i) \(i)")
if loopNumber > 1 {
loopNumber -= 1
previous_i = i
loop(start: previous_i+1, upTo: n+loopNumber)
}
}
}
loop(start: ix, upTo: n)
}
multiLoopsRecursive(start: 3, upTo: 6, loopNumber: 2)
With the following result ...
it is 3 4
it is 4 6
it is 4 7
it is 4 5
it is 4 6
Not really the same as the basic double loop ...
I am really blocked...
Thank you for your help...
Regards
K
Basic double loop with a print loopNumber added:
for i in ix...n {
print("it is loop 1")
for j in i+1 ... n+1 {
print("it is loop 2")
print("it is \(i) \(j)")
}
}
}
multiLoops(start: 3, upTo: 6)
Result:
it is loop 1
it is loop 2
it is 3 4
it is loop 2
it is 3 5
it is loop 2
it is 3 6
it is loop 2
it is 3 7
it is loop 1
it is loop 2
it is 4 5
it is loop 2
it is 4 6
it is loop 2
it is 4 7
it is loop 1
it is loop 2
it is 5 6
it is loop 2
it is 5 7
it is loop 1
it is loop 2
it is 6 7
Larme, Please find your code with the loopNumber added:
var loopNumber = 1
func multiLoopsRecursive(start: Int, upTo: Int) {
print("it is loop \(loopNumber)")
for i in start+1...upTo+1 {
print("it is \(start) \(i)")
}
let newStart = start + 1
if newStart < upTo+1 {
loopNumber += 1
multiLoopsRecursive(start: newStart, upTo: upTo)
}
}
print("Recursive")
multiLoopsRecursive(start: 3, upTo: 6)
Result:
Recursive
it is loop 1
it is 3 4
it is 3 5
it is 3 6
it is 3 7
it is loop 2
it is 4 5
it is 4 6
it is 4 7
it is loop 3
it is 5 6
it is 5 7
it is loop 4
it is 6 7
Thank you for your support
K
Here we are !
The trick is to know in which loop you are to adapt the different variables. I leave in the code the « print loop down and up » for more clarity, but it can be suppressed of course.
Now it works with loops in loops :-)
func loopInLoop (iStart:Int, iEnd:Int, numberOfLoops : Int) {
print("what loop is it! \(loopIndex)")
var iRank = 0
for i in iStart...(iEnd - numberOfLoops + loopIndex) {
iArray[loopIndex-1] = i
iRank += 1
if loopIndex < numberOfLoops {
print ("loop down")
loopIndex += 1
loopInLoop(iStart: iStart + iRank, iEnd: iEnd, numberOfLoops: numberOfLoops)
} else {print("iArray:\(iArray)")}
}
print ("loop up")
loopIndex -= 1
}
let iStart = 3 // initial starting index of first loop, must be <= iEnd
let iEnd = 7 // ending starting index of first loop
let numberOfLoops = 2 // number of loop to execute, must be <= (iEnd-iStart)
var iArray = [Int](repeating: 0, count: numberOfLoops) // Array of indexes
var loopIndex = 1 // initial index of the loop
loopInLoop(iStart: iStart, iEnd: iEnd, numberOfLoops : numberOfLoops)
Result:
what loop is it! 1
loop down
what loop is it! 2
iArray:[3, 4]
iArray:[3, 5]
iArray:[3, 6]
iArray:[3, 7]
loop up
loop down
what loop is it! 2
iArray:[4, 5]
iArray:[4, 6]
iArray:[4, 7]
loop up
loop down
what loop is it! 2
iArray:[5, 6]
iArray:[5, 7]
loop up
loop down
what loop is it! 2
iArray:[6, 7]
loop up
loop up

How to do a permutation with restrictions?

I have a vector using Matlab whose values are from 1 to 18, I need to perform permutations of those values considering that certain numbers cannot go together. For example:
vector = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18]
In the conditions I have that the following numbers cannot go together:
1 and 7
1 and 8
1 and 17
1 and 18
2 and 6
2 and 7
2 and 8
2 and 17
2 and 18
So a valid permutation could be:
[1 2 4 3 5 6 7 8 9 10 11 12 13 14 15 17 16 18]
An invalid permutation could be:
[1 7 4 3 5 6 2 8 9 10 11 12 13 14 15 17 16 18] 1 and 7 together
[1 8 4 3 5 6 2 7 9 10 11 12 13 14 15 17 16 18] 1 and 8 together
[1 17 4 3 5 6 2 8 9 10 11 12 13 14 15 7 16 18] 1 and 17 together
These conditions must apply in any position of the vector.
I have no ideas how to do this, if someone could give me ideas I would really appreciate it.
Taking into account a previous comment, my code generates a random permutation of your vector and checks that all these combinations are not in the permutation. If some condition is found a new permutation is generated:
vector = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18];
not_valid = 1;
while not_valid == 1
randpermutation = vector(randperm(18))
not_valid = 0;
for i = 1:length(randpermutation)-1
if randpermutation(i) == 1 && randpermutation(i+1) == 7 || randpermutation(i) == 7 && randpermutation(i+1) == 1
not_valid = 1;
end
if randpermutation(i) == 1 && randpermutation(i+1) == 8 || randpermutation(i) == 8 && randpermutation(i+1) == 1
not_valid = 1;
end
if randpermutation(i) == 1 && randpermutation(i+1) == 17 || randpermutation(i) == 17 && randpermutation(i+1) == 1
not_valid = 1;
end
if randpermutation(i) == 1 && randpermutation(i+1) == 18 || randpermutation(i) == 18 && randpermutation(i+1) == 1
not_valid = 1;
end
if randpermutation(i) == 2 && randpermutation(i+1) == 7 || randpermutation(i) == 7 && randpermutation(i+1) == 2
not_valid = 1;
end
if randpermutation(i) == 2 && randpermutation(i+1) == 8 || randpermutation(i) == 8 && randpermutation(i+1) == 2
not_valid = 1;
end
if randpermutation(i) == 2 && randpermutation(i+1) == 17 || randpermutation(i) == 17 && randpermutation(i+1) == 2
not_valid = 1;
end
if randpermutation(i) == 2 && randpermutation(i+1) == 18 || randpermutation(i) == 18 && randpermutation(i+1) == 2
not_valid = 1;
end
if randpermutation(i) == 2 && randpermutation(i+1) == 6 || randpermutation(i) == 6 && randpermutation(i+1) == 2
not_valid = 1;
end
end
end

Getting NALU from H.264 RTSP-Stream with FFmpeg and VideoToolbox in Swift

Im very new to video processing and now I am stuck decoding my H.264 RTSP-Stream with FFmpeg and VideoToolbox in Swift.
Currently I am a bit overwhelmed extracting sps and pps
-> Where are they stored? I have the following options getting data
- AVFrame.data
- AVFrame.extended_data
- AVFrame.metadata
- AVPacket.data
- AVPacket.side_data
- AVCodecContext.extra_data
.. and so on
For now I am working with AVCodecContext.extra_data, but this seems a bit different to the example from here
My code for getting SPS and PPS is this one
private func receiveRawFrame(frame:AVFrame,codecContext:AVCodecContext){
//Get the extradata, where the SPS and the PPS is stored?
let codecContextExtraData:UnsafeMutablePointer<UInt8> = codecContext.extradata
let startCodeIndex = 0
var secondStartCodeIndex = 0
var thirdStartCodeIndex = 0
var naluType = self.getNaluType(naluTypeRaw: codecContextExtraData[startCodeIndex + 4] & 0x1F)
if naluType == .sps{
print("Yeah SPS")
for i in startCodeIndex+4...startCodeIndex + 40{
if (codecContextExtraData[Int(i)] == 0x00 && codecContextExtraData[Int(i)+1] == 0x00 && codecContextExtraData[Int(i)+2] == 0x00 && codecContextExtraData[Int(i)+3] == 0x01){
secondStartCodeIndex = i
spsSize = i
break
}
}
let secondNaluTypeRaw = (codecContextExtraData[Int(secondStartCodeIndex) + 4] & 0x1F)
naluType = self.getNaluType(naluTypeRaw: secondNaluTypeRaw)
}
if naluType == .pps{
print("Yeah PPS")
for i in (spsSize+4)..<(spsSize+30){
if (codecContextExtraData[Int(i)] == 0x00 && codecContextExtraData[Int(i)+1] == 0x00 && codecContextExtraData[Int(i)+2] == 0x00 && codecContextExtraData[Int(i)+3] == 0x01){
print("Never gets here")
break
}
}
}
else{
print("other -> TBD")
}
}
}
Further function to get the naluType:
private func getNaluType(naluTypeRaw:UInt8) -> NaluType {
switch naluTypeRaw {
case 0: return .pframe
case 5: return .iframe
case 7: return .sps
case 8: return .pps
default:
return .unknown
}
}
With this custom enumerator:
enum NaluType {
case sps
case pps
case pframe
case iframe
case unknown
}
As you can see in the comment of the receiveRawFrame function, I never get the third NALU. When I print the AVCodecContext.extraData from [0] to [50] I get the following output
0
0
0
1
103
66
192
30
217
3
197
104
64
0
0
3
0
64
0
0
12
3
197
139
146
0
0
0
1
104
203
140
178
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Now it makes sense, that I never get the third NALU, because there are only 2 StartCodes, but where is the rest?

Storing data in matrix in for and if loops

I have a problem in storing the data in matrix in for and if loops,
The results give me only the last value of the last iteration. I want all the
results of all iterations to be stored in a matrix be sequence.
Here is a sample of my code:
clear all
clc
%%%%%%%%%%%%%%
for M=1:3;
for D=1:5;
%%%%%%%%%%%%%%
if ((M == 1) && (D <= 3)) || ((M == 3) && (2 <= D && D <= 5))
U1=[5 6];
else
U1=[0 0];
end
% desired output:
% U1=[5 6 5 6 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 5 6 5 6 5 6]
%%%%%%%%%%%%%%
if (M == 1) && (D==4) || ((M == 3) && (D == 1))
U2=[8 9];
else
U2=[0 0];
end
% desired output:
% U2=[0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0]
%%%%%%%%%%%%%%
if ((M == 1) && (D == 5)) || ((M == 2) && (1 <= D && D <= 5))
U3=[2 6];
else
U3=[0 0];
end
% desired output:
% U3=[0 0 0 0 0 0 0 0 2 6 2 6 2 6 2 6 2 6 2 6 0 0 0 0 0 0 0 0 0 0]
%%%%%%%%%%%%%%
end
end
You are overwriting your matrices each time you write UX=[X Y];.
If you want to append data, either preallocate your matrices and specify the matrix index each time you assign a new value, or write UX=[UX X Y]; to directly append data at the end of your matrices.
clear all
clc
U1=[];
U2=[];
U3=[];
for M=1:3
for D=1:5
if ((M == 1) && (D <= 3)) || ((M == 3) && (2 <= D && D <= 5))
U1=[U1 5 6];
else
U1=[U1 0 0];
end
% desired output:
% U1=[5 6 5 6 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 5 6 5 6 5 6]
if (M == 1) && (D==4) || ((M == 3) && (D == 1))
U2=[U2 8 9];
else
U2=[U2 0 0];
end
% desired output:
% U2=[0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0]
if ((M == 1) && (D == 5)) || ((M == 2) && (1 <= D && D <= 5))
U3=[U3 2 6];
else
U3=[U3 0 0];
end
% desired output:
% U3=[0 0 0 0 0 0 0 0 2 6 2 6 2 6 2 6 2 6 2 6 0 0 0 0 0 0 0 0 0 0]
end
end
You can avoid the loops altogether:
[M,D] = meshgrid(1:3,1:5);
M = M(:)'; D = D(:)';
idx1 = ( M==1 & D<=3 ) | ( M== 3 & 2<=D & D<=5 );
idx2 = ( M==1 & D==4) | ( M==3 & D==1 );
idx3 = ( M==1 & D==5 ) | ( M==2 & 1<=D & D<=5 );
U1 = bsxfun(#times, idx1, [5;6]); U1 = U1(:)';
U2 = bsxfun(#times, idx2, [8;9]); U2 = U2(:)';
U3 = bsxfun(#times, idx3, [2;6]); U3 = U3(:)';