matlab loops for converting unit - matlab

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

Related

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?

How can I compare a boolean to an array? - 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)

Whats a boolean equation that is true when exclusively one variable is true?

I have 4 variables this was the best I could do, but it returns true if there's 3 true variables.
if(a ^ b ^ c ^ d)
If you're talking C, where booleans are simply 0/1 values, you can use:
a + b + c + d == 1
Otherwise, provided you can use && and ||:
( a && !b && !c && !d) ||
(!a && b && !c && !d) ||
(!a && !b && c && !d) ||
(!a && !b && !c && d)
This simply breaks it down to the four possible cases where each has only one variable set.
Now you can "simplify" that with Karnaugh maps as follows (all blanks are false):
AB
0 0 1 1
0 1 0 1
+---+---+---+---+
CD 00 | | T | T | |
+---+---+---+---+
01 | T | | | |
+---+---+---+---+
10 | T | | | |
+---+---+---+---+
11 | | | | |
+---+---+---+---+
to be:
((a ^ b) && !c && !d) || ((c ^ d) && !a && !b)
but it's a lot harder to understand (and debug) than the simpler expression above, and doesn't really save much so, unless you well versed in boolean algebra, DeMorgan's Theorem, Karnaugh maps and such, stick with the first one.
Just to show that the expressions are equivalent, consider the following C code for checking all possibilities:
#include <stdio.h>
int main (void) {
int a, b, c, d, x, y;
for (a = 0; a < 2; a++) {
for (b = 0; b < 2; b++) {
for (c = 0; c < 2; c++) {
for (d = 0; d < 2; d++) {
x =
( a && !b && !c && !d) ||
(!a && b && !c && !d) ||
(!a && !b && c && !d) ||
(!a && !b && !c && d);
y = ((a ^ b) && !c && !d) || ((c ^ d) && !a && !b);
printf ("%d %d %d %d | %d %d | %s\n",
a, b, c, d, x, y, (x == y) ? "Okay" : "Bad");
}
}
}
}
return 0;
}
This outputs all 16 possibilities, showing that the two expressions generate the same outcome:
0 0 0 0 | 0 0 | Okay
0 0 0 1 | 1 1 | Okay
0 0 1 0 | 1 1 | Okay
0 0 1 1 | 0 0 | Okay
0 1 0 0 | 1 1 | Okay
0 1 0 1 | 0 0 | Okay
0 1 1 0 | 0 0 | Okay
0 1 1 1 | 0 0 | Okay
1 0 0 0 | 1 1 | Okay
1 0 0 1 | 0 0 | Okay
1 0 1 0 | 0 0 | Okay
1 0 1 1 | 0 0 | Okay
1 1 0 0 | 0 0 | Okay
1 1 0 1 | 0 0 | Okay
1 1 1 0 | 0 0 | Okay
1 1 1 1 | 0 0 | Okay
You can write out your expression explicitly. In the long form, for four boolean variables, it would be (!a && !b && !c && d) || (!a && !b && c && !d) || (!a && b && !c && !d) || (a && !b && !c && !d)
For an arbitrary number of variables, for any arbitrary boolean functions, you can use a truth table to get the appropriate boolean expression and the karnaugh map to simplify it.
May be you wanna try ternary operator as the execution time for this is slightly less, even when we consider the best case scenario of the above solutions.
(a?(b?false:(c?false:(d?false:true))):(b?(c?false:(d?false:true)):(c?(d?false:true):(d?true:false))))

Linear Impulses - Cocos2d/Objective-C/Box2d

I have a cannon, a ball, and a trigger. When you press the trigger a linear impulse is applied to the ball.
short int direction = [level.cannon cannon].rotation;
short int power = 24.8;
b2Vec2 force = b2Vec2(direction, power);
[level.ball body]->ApplyLinearImpulse(force, [level.ball body]->GetWorldCenter());
My problem is, when the trigger is pressed, the linear impulse is applied to the ball, but the ball doesn't actually come out of the top of the cannon sprite.
The reason it is doing this (I think) is because I have set the anchor point, for the cannon, to (0.5, 0).
cannon.anchorPoint = ccp(0.5, 0);
I have figured out that for every 5 degrees the cannon rotates, a multiple of 2 needs to be added/subtracted to the rotation.
E.G.
For 0 to 55 Degrees
0 to 5 subtract 2 from the rotation
5 to 10 subtract 4 from the rotation
10 to 15 subtract 6 from the rotation
etc.
For 0 to -55 Degrees
0 to -5 add 2 to the rotation
-5 to -10 add 4 to the rotation
-10 to -15 add 6 to the rotation
Currently, I am using this code to accomplish this.
if (_cannon.rotation == 0)
{
direction = _cannon.rotation;
} else if (_cannon.rotation >= 1 && _cannon.rotation < 6)
{
direction = _cannon.rotation - 2;
} else if (_cannon.rotation >= 6 && _cannon.rotation < 11)
{
direction = _cannon.rotation - 4;
} else if (_cannon.rotation >= 11 && _cannon.rotation < 16)
{
direction = _cannon.rotation - 6;
} else if (_cannon.rotation >= 16 && _cannon.rotation < 21)
{
direction = _cannon.rotation - 8;
} else if (_cannon.rotation >= 21 && _cannon.rotation < 26)
{
direction = _cannon.rotation - 10;
} else if (_cannon.rotation >= 26 && _cannon.rotation < 31)
{
direction = _cannon.rotation - 12;
} else if (_cannon.rotation >= 31 && _cannon.rotation < 36)
{
direction = _cannon.rotation - 14;
} else if (_cannon.rotation >= 36 && _cannon.rotation < 41)
{
direction = _cannon.rotation - 16;
} else if (_cannon.rotation >= 41 && _cannon.rotation < 46)
{
direction = _cannon.rotation - 18;
} else if (_cannon.rotation >= 46 && _cannon.rotation < 55)
{
direction = _cannon.rotation - 20;
} else if (_cannon.rotation <= -1 && _cannon.rotation > -6)
{
direction = _cannon.rotation + 2;
} else if (_cannon.rotation <= -6 && _cannon.rotation > -11)
{
direction = _cannon.rotation + 4;
} else if (_cannon.rotation <= -11 && _cannon.rotation > -16)
{
direction = _cannon.rotation + 6;
} else if (_cannon.rotation <= -16 && _cannon.rotation > -21)
{
direction = _cannon.rotation + 8;
} else if (_cannon.rotation <= -21 && _cannon.rotation > -26)
{
direction = _cannon.rotation + 10;
} else if (_cannon.rotation <= -26 && _cannon.rotation > -31)
{
direction = _cannon.rotation + 12;
} else if (_cannon.rotation <= -31 && _cannon.rotation > -36)
{
direction = _cannon.rotation + 14;
} else if (_cannon.rotation <= -36 && _cannon.rotation > -41)
{
direction = _cannon.rotation + 16;
} else if (_cannon.rotation <= -41 && _cannon.rotation > -46)
{
direction = _cannon.rotation + 18;
} else if (_cannon.rotation <= -46 && _cannon.rotation > -55)
{
direction = _cannon.rotation + 20;
}
I know there has to be an easier way to do this, but I'm still learning. Can someone please help me?
short int val, val2;
val = _cannon.rotation/5;
val2 = _cannon.rotation%5; //remainder of the division
//it will be zero when rotation is a multiple of 5.
if(val2 > 0)
val += 1;
direction = _cannon.rotation - 2*val;
I'm not sure if this is what you want, and I didn't test it. Just made some mental tests to see if it works for the cases you put.
But I hope you can use this as a starting point for making your code smaller.

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(:)';