I am trying to understand Computed Properties mostly I have understood the concept but one output is confusing me
struct SomePrices {
var eighth: Double
var quarter: Double
var half: Double
var zip: Double {
get {
return half * 2 - 20
}
set {
eighth = newValue / 8 + 15
quarter = newValue / 4 + 10
half = newValue / 2 + 5
}
}
}
var gdp = SomePrices(eighth: 37.0, quarter: 73.0, half: 123.0)
gdp.eighth // 37
gdp.quarter // 73
gdp.half // 123
gdp.zip // 226
gdp.zip = 300
gdp.eighth // 52.5
gdp.quarter // 85
gdp.half // 155
gdp.zip // 290
Been trying to understand how did I get 290 when gdp.zip = 300
You set zip to 300 so half becomes (300 / 2 + 5) = 155.
half = newValue / 2 + 5
Then you get zip which is (155 * 2 - 20) = 290.
return half * 2 - 20
Related
How do I archive this result using a simple mathematical formula.
I have an initial offset = 100, and initial count = 0, that i want to increase the offset based on count value. I tried using the below code but it doesn't function correctly.
Example
When count is 0 to 3, then offset should be 100.
When count is
4 to 6, then offset should be 200.
When count is 7 to 9,
then offset should be 300.
When count is 10 to 12, then offset
should be 400.
Attempt
func getHeight(count: Int) ->CGFloat {
var index = 0
for i in 0..<count{
if(i % 2 != 0){
index += 1
}
}
return CGFloat(100 * index)
//return CGFloat(count + 3 / 9 * 100)
}
Testing
print("0 to 3 = \(self.getHeight(count: 0)), expected = 100")
print("0 to 3 = \(self.getHeight(count: 2)), expected = 100")
print("4 to 6 = \(self.getHeight(count: 4)), expected = 200")
print("7 to 9 = \(self.getHeight(count: 7)), expected = 300")
print("10 to 12 = \(self.getHeight(count: 12)), expected = 400")
Results
0 to 3 = 0.0, expected = 100
0 to 3 = 100.0, expected = 100
4 to 6 = 200.0, expected = 200
7 to 9 = 300.0, expected = 300
10 to 12 = 600.0, expected = 400
Formula with integer division:
let cnt = count != 0 ? count : 1
result = 100 * ((cnt + 2) / 3)
How do I round down number nearest 100 with dart? e.g;
43 -> 100
153 -> 200
123 -> 200
450 -> 500
399 -> 400
1234 -> 1300
3456 -> 3500
int calculateNumber(int number) {
int a = number % 100;
if (a > 0) {
return (number ~/ 100) * 100 + 100;
}
return number;
}
Approach 1
Can Use integer division, which truncates the decimal portion of the quotient.
int result = ((number + 99) / 100 ) * 100;
Approach 2
(int) (Math.ceil(number/100.0))*100
How do you accurately take the average of large sets of integers in MATLAB?
I have two large vectors (2672x4008 in dimensions) I am dealing with, each the result of pixels in an image. Hence, the resulting vector is filled with values 0 to 256, all integers. My problem is that I want an accurate value of the average intensity of these grey-scale images. To do this, I used the line
meanvalue = mean(I(:))
This yielded a value of meanvalue = 155.9335 in the output line of MATLAB.
Next, I added 20 to each value of the vector, as below (this should raise the intensity of the overall image, if I am understanding correctly).
Ipt = I + 20;
I then took the mean value of this new vector, Ipt
meanvaluept = mean(Ipt(:))
and matlab spat out a value of meanvaluept = 175.8916. I'm no math wizard, but I know enough to know that 175.8916 - 20 ≠ 155.9335.
Any help would be appreciated, either mathematically (how to increase the precision of MATLAB), or procedurally (there is some built-in function of MATLAB which will find the intensity).
Since you are referring to "grey-scale images", and you have integers in the range 0-255 (the 256 you mention must be a typo), my guess is that your I is of type uint8.
In this case, MATLAB uses saturated addition, in which results larger than 255 are clamped to 255. The effect you describe is caused by this saturated addition.
Here is an example:
>> I = uint8(randi(255,1000,1000));
>> mean( I(:)+20 )
ans =
147.1954
>> mean(I(:)) + 20
ans =
148.0151
The solution is to convert to doubles first:
>> mean( double(I(:)) + 20 )
ans =
148.0151
Have you checked the image datatype?
It's true that if your the mean of image I is
meanvalue = mean(I(:)) = 155.9335
and you added 20 to each pixels
Ipt = I + 20
you supposed to have
meanept = mean(Ipt(:)) = meanvalue + 20 = 175.9335
But, don't forget that image's datatype is uint8, which limit the pixels value to 0-255. It means if you added 20 to a pixel and its value is greater than 255, its value will set to 255, and the same if you substract some value and it's lower than 0.
Maybe, some of your pixels restricted to 255 when normally you'll have more than 255.
For example:
I have vector X in double
X = [1 1 1; ...
1 1 1; ...
1 1 240];
The mean of X is
mean(X(:)) = 27.5556
since
( 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 240)/9 = 27.5556
If I added 20 to each pixels
X20 = X + 20
= [(1 + 20) (1 + 20) (1 + 20); ...
(1 + 20) (1 + 20) (1 + 20); ...
(1 + 20) (1 + 20) (240 + 20)];
= [21 21 21; ...
21 21 21; ...
21 21 255];
Notice that X20(3,3) is 255, not 260. It cause
meanX20 = mean(X20(:)) = 47
but if I change X's datatype to double
X_double = double(X)
and added 20 to each pixels
X20_double = X_double + 20
= [(1 + 20) (1 + 20) (1 + 20); ...
(1 + 20) (1 + 20) (1 + 20); ...
(1 + 20) (1 + 20) (240 + 20)];
= [21 21 21; ...
21 21 21; ...
21 21 260];
and the average of X20_double is
X20_double_mean = mean(X20_double(:)) = 47.5556
See the difference?
The double X20's mean is 47.5556 and the uint8 X20's mean is 47.
I hope this will help :)
There is a very important note in your question :
Suppose I = [2 3;4 9]
meanvalue = mean(I(:)) = 4.5
When you add 20 with I , You will have :
Ipt = I + 20;
Ipt = [22 23;24 29]
so you add 20 to all elements in I, therefore your mean will increase 20 value.
I have a minimal MacOS app (one view controller + one button) with the following code (basically a copy-paste from AudioKit's playground):
public class Player {
static let playRate = 2.0
static let scale = [0, 2, 4, 5, 7, 9, 11, 12]
var pluckedString: AKPluckedString! = nil
var delay: AKDelay! = nil
var reverb: AKReverb! = nil
var performance: AKPeriodicFunction! = nil
public init() {
pluckedString = AKPluckedString()
delay = AKDelay(pluckedString) // <- objc_exception_throw here
delay.time = 1.5 / Player.playRate
delay.dryWetMix = 0.3
delay.feedback = 0.2
reverb = AKReverb(delay)
performance = AKPeriodicFunction(frequency: Player.playRate) {
var note = Player.scale.randomElement()
let octave = [2, 3, 4, 5].randomElement() * 12
if random(0, 10) < 1.0 { note += 1 }
if !Player.scale.contains(note % 12) { print("ACCIDENT!") }
let frequency = (note + octave).midiNoteToFrequency()
if random(0, 6) > 1.0 {
self.pluckedString.trigger(frequency: frequency)
}
}
}
}
The problem is that call to AKDelay(pluckedString) produces ObjC exception:
AKPluckedString.swift:init(frequency:amplitude:lowestFrequency:):94:Parameter Tree Failed
[avae] AVAEInternal.h:70:_AVAE_Check: required condition is false: [AVAudioEngine.mm:353:AttachNode: (node != nil)]
[General] required condition is false: node != nil
[General] (
0 CoreFoundation 0x00007fff49d8d00b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff7096bc76 objc_exception_throw + 48
2 CoreFoundation 0x00007fff49d92da2 +[NSException raise:format:arguments:] + 98
3 AVFAudio 0x00007fff4610b75e _Z19AVAE_RaiseExceptionP8NSStringz + 158
4 AVFAudio 0x00007fff460ab1a2 _Z11_AVAE_CheckPKciS0_S0_b + 330
5 AVFAudio 0x00007fff4611f2e7 _ZN17AVAudioEngineImpl10AttachNodeEP11AVAudioNodeb + 63
6 AVFAudio 0x00007fff4611f267 -[AVAudioEngine attachNode:] + 67
7 AudioKit 0x000000010051de01 globalinit_33_0214DCBA62A4B4A95DF14CC0DE6A86C6_func60 + 13249
8 AudioKit 0x000000010051f24d globalinit_33_0214DCBA62A4B4A95DF14CC0DE6A86C6_func60 + 18445
9 AudioKit 0x0000000100512284 block_copy_helper.12 + 4852
10 AudioKit 0x0000000100519119 block_copy_helper.12 + 33161
11 AudioKit 0x0000000100639e8f block_copy_helper.12 + 38463
12 AudioKit 0x00000001006397ca block_copy_helper.12 + 36730
...
How can I fix this?
I am using AudioKit 4.0.4 / Swift 4.0.3 / XCode 9.2 (9C40b).
Is it because your App Sandbox is on? My Xcode (9.2) is defaulting to App Sandbox on (Capabilities tab of project). This results in a AudioKit giving this error.
Good morning,
I'm sure there's a built in function but I can't find it. I want to create static positioning for information being sent to a text document in MATLAB. For example:
height weight age favorite number
------------------------------------------------------------
60 140 24 9
30 45 3 10000000
48 100 9 19
9 7 1 1
currently, i'm just doing an fprint call with padded spaces to get it lined up, but the issue arises where having different length numbers causes the alignment to be off, like so:
height weight age favorite number
------------------------------------------------------------
60 140 24 9
30 45 3 10000000
48 100 9 19
1 7 1 1
Thanks in advance.
here's an example script that'll show what I mean:
fid1 = fopen('stackoverflowtest', 'w');
if fid1 < 3,
error('ERROR');
end;
fprintf(fid1, 'height weight age favorite number \n');
fprintf(fid1, '------------------------------------------------------------ \n');
height = 0;
weight = 10;
age = 100;
number = 3;
for i = 1:100
fprintf(fid1, "%d ', height);
fprintf(fid1, "%d ', weight);
fprintf(fid1, "%d ', age);
fprintf(fid1, "%d \n", number);
height = height + 3;
weight = weight + 6;
age = age - 1;
number = number + 23;
end
You can do this with the fprintf format specification, for example %-15d.
Here, the - is a flag which specifies left justification, and the 15 specifies how much space to leave around the representation.
We can reproduce your example with
A = [60 140 24 9
30 45 3 10000000
48 100 9 19
9 7 1 1];
fprintf('height weight age favorite number \n'),...
fprintf('------------------------------------------------------------ \n'),...
fprintf('%-15d %-15d %-12d %-15d \n',A')
which displays
height weight age favorite number
------------------------------------------------------------
60 140 24 9
30 45 3 10000000
48 100 9 19
9 7 1 1
EDIT: You can store this data as a table:
height = A(:,1)
weight = A(:,2);
age = A(:,3);
favourite_number = A(:,4);
tab1 = table(height, weight, age, favourite_number);
disp(tab1);
This prints to screen
height weight age favourite_number
______ ______ ___ ________________
60 140 24 9
30 45 3 1e+07
48 100 9 19
9 7 1 1
but I'm not sure how to save this representation to a file.