Adding two formulas in Crystal Reports - crystal-reports

I know I'm not connecting something somewhere, so I need your help.
I have made calculations in each formula and now need to add them together - simple, but not. haha
Here is my code:
Group 1 - student
Group 2 - class
Group 3 - StudentWorkList_.Excuse Type (which is divided by Absent and Tardy)
I have to add the Absences and Tardies together, but for every 3 tardies = 1 Absent.
The calculations come out correctly for each one separately, but I can't add the Absence formula + the Tardy to Absent formula.
Creating another formula with Absent + Tardy to Absent does nothing...
Formulas:
Absent:
if GroupName ({StudentWorkList_.Excuse Type}) = "Absent" then Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type})/76*100
Tardy to Absent:
if GroupName ({StudentWorkList_.Excuse Type}) = "Tardy" then
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 3 then 1 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 4 then 1 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 5 then 1 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 6 then 2 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 7 then 2 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 8 then 2 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 9 then 3 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 10 then 3 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 11 then 3 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 12 then 4 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 13 then 4 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 14 then 4 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 15 then 5 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 16 then 5 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 17 then 5 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 18 then 6 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 19 then 6 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 20 then 6 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 21 then 7 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 22 then 7 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 23 then 7 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 24 then 8 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 25 then 8 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 26 then 8 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 27 then 9 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 28 then 9 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 29 then 9 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 30 then 10 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 31 then 10 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 32 then 10 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 33 then 11 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 34 then 11 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 35 then 11 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 36 then 12 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 37 then 12 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 38 then 12 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 39 then 13 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 40 then 13 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 41 then 13 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 42 then 14 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 43 then 14 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 44 then 14 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 45 then 15 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 46 then 15 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 47 then 15 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 48 then 16 /76100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 49 then 16 /76*100 else
if Count ({StudentWorkList_.Excuse Type}, {StudentWorkList_.Excuse Type}) = 50 then 16

Create a simple detail level formula called {#Absence_Equivalent} using an expression such as:
IF {StudentWorkList_.Excuse Type}) = "Absent" Then 1
ELSE IF {StudentWorkList_.Excuse Type}) = "Tardy" Then 1/3
ELSE 0
Then, SUM that formula at any grouping or grand total level you wish.

Option 1: Create a formula that divides the sum by 76 -- that seems like the simplest approach. If for some reason you don't want to do that, then:
Option 2: divide the detail level value by 76. For example,
IF {StudentWorkList_.Excuse Type}) = "Absent" Then 1/76
ELSE IF {StudentWorkList_.Excuse Type}) = "Tardy" Then 1/228
ELSE 0

Related

while loop in my program does not fill the second row

I have 2 inner while loops. Code writes the results to first row on the matrix but when it gets to second row, it just passes the other rows and doesn't fill the columns of the rows. How to solve it? The code and output are like below:
while i <= m-1
i
while a <= m-1
a
den1 = sqrt(((xy{i,j}-xy{a+1,b})^2+(xy{i,j+1}-xy{a+1,b+1})^2 ));
dMat(i,a) = den1;
a = a+1;
end
i = i+1;
end
i = 1
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9
a = 10
a = 11
a = 12
a = 13
a = 14
a = 15
a = 16
a = 17
a = 18
a = 19
a = 20
a = 21
a = 22
a = 23
a = 24
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
i = 11
i = 12
i = 13
i = 14
i = 15
i = 16
i = 17
i = 18
i = 19
i = 20
i = 21
i = 22
i = 23
i = 24
You have to restart a to a=1 for each iteration. Put it above the while a <= m-1

one-by-one matrix assignment MATLAB

I need to find the minimum values in each column of matrix "A", and then replace those min values with the values in last row of matrix "B" (which has same number of columns). Like I have these:
>> A = randi(10,10,5)
A =
3 5 9 5 8
7 6 4 10 2
8 4 1 7 4
4 7 2 8 2
7 5 8 7 5
3 7 10 10 1
5 7 8 5 7
8 3 8 2 3
6 10 2 1 10
3 7 6 7 2
>> B = randi(100,3,5)
B =
10 34 66 18 62
99 95 49 54 81
52 1 52 9 95
>> [M,I] = min(A)
M =
3 3 1 1 1
I =
1 8 3 9 6
And I want to replace the values of "M" with "B(end,:), so that:
A(1,1) = B(end,1);
A(8,2) = B(end,2);
A(3,3) = B(end,3);
A(9,4) = B(end,4);
A(6,5) = B(end,5);
I try "A(I) = B(end,:)" and "A(I(1,:)) = B(end,:)" but they do not work! Any ideas how I could do that? My real matrices are huge (1200x100000) so no way to do it by hand!
try this to replace the first min value:
A = [ 3 5 9 5 8;
7 6 4 10 2;
8 4 1 7 4;
4 7 2 8 2;
7 5 8 7 5;
3 7 10 10 1;
5 7 8 5 7;
8 3 8 2 3;
6 10 2 1 10;
3 7 6 7 2];
B =[ 10 34 66 18 62;
99 95 49 54 81;
52 1 52 9 95];
[M,I] = min(A)
A(sub2ind(size(A),I,1:size(A,2)))=B(end,:)
the output will be:
A =
52 5 9 5 8
7 6 4 10 2
8 4 52 7 4
4 7 2 8 2
7 5 8 7 5
3 7 10 10 95
5 7 8 5 7
8 1 8 2 3
6 10 2 9 10
3 7 6 7 2
However, when you have to replace all of the min values, use the code below instead
A = [ 3 5 9 5 8;
7 6 4 10 2;
8 4 1 7 4;
4 7 2 8 2;
7 5 8 7 5;
3 7 10 10 1;
5 7 8 5 7;
8 3 8 2 3;
6 10 2 1 10;
3 7 6 7 2];
B =[ 10 34 66 18 62;
99 95 49 54 81;
52 1 52 9 95];
M = min(A);
for i=1:size(A,2)
A(find(A(:,i) == M(i)),i)=B(end,i);
end;
A
the output is:
A =
52 5 9 5 8
7 6 4 10 2
8 4 52 7 4
4 7 2 8 2
7 5 8 7 5
52 7 10 10 95
5 7 8 5 7
8 1 8 2 3
6 10 2 9 10
52 7 6 7 2
You can acces you matrix by a single index, which looks like this:
Indeces =
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
Since you get the indeces for each individual column, you just need to increase it by the column number times the height of the matrix.
This should yield the correct result:
A( I + (0 : size(A,2)-1) * size(A,1) ) = B(end,:)

How to remove all the rows from a matrix that match values in another vector?

I am making an exclude vector, so that the rows containing any value present in the second column of the matrix user from the exclude list are removed. How do I do that efficiently, without using a for loop to iterate through user for each item in exclude one by one?
My code below does not work:
count=0;
% Just showing how I am constructing `exclude`, to show that it can be long.
% So, manually removing each item from `exclude` is not an option.
% And using a for loop to iterate through each element in `exclude` can be inefficient.
for b=1:size(user_cat,1)
if user_cat(b,4)==0
count=count+1;
exclude(count,1) = user_cat(b,1);
end
end
% This is the important line of focus. You can ignore the previous parts.
user = user(user(:,2)~=exclude(:),:);
The last line gives the following error:
Error using ~=
Matrix dimensions must agree.
So, I am having to use this instead:
for b=1:size(exclude,1)
user = user(user(:,2)~=exclude(b,1),:);
end
Example:
user=[1433100000.00000 26 620260 7 1433100000000.00 0 0 2 1 100880 290 23
1433100000.00000 26 620260 7 1433100000000.00 0 0 2 1 100880 290 23
1433100000.00000 25 620160 7 1433100000000.00 0 0 2 1 100880 7274 22
1433100000.00000 21 619910 7 1433100000000.00 24.1190000000000 120.670000000000 2 0 100880 53871 21
1433100000.00000 19 620040 7 1433100000000.00 24.1190000000000 120.670000000000 2 0 100880 22466 21
1433100000.00000 28 619030 7 1433100000000.00 24.6200000000000 120.810000000000 2 0 100880 179960 16
1433100000.00000 28 619630 7 1433100000000.00 24.6200000000000 120.810000000000 2 0 100880 88510 16
1433100000.00000 28 619790 7 1433100000000.00 24.6200000000000 120.810000000000 2 0 100880 12696 16
1433100000.00000 7 36582000 7 1433100000000.00 0 0 2 0 100880 33677 14
1433000000.00000 24 620010 7 1433000000000.00 0 0 2 1 100880 3465 14
1433000000.00000 4 36581000 7 1433000000000.00 0 0 2 0 100880 27809 12
1433000000.00000 20 619960 7 1433000000000.00 0 0 2 1 100880 860 11
1433000000.00000 30 619760 7 1433000000000.00 25.0060000000000 121.510000000000 2 0 100880 34706 10
1433000000.00000 33 619910 7 1433000000000.00 0 0 2 0 100880 15060 9
1433000000.00000 26 619740 6 1433000000000.00 0 0 2 0 100880 52514 8
1433000000.00000 18 619900 6 1433000000000.00 0 0 2 0 100880 21696 8
1433000000.00000 16 619850 6 1433000000000.00 24.9910000000000 121.470000000000 2 0 100880 10505 1
1433000000.00000 16 619880 6 1433000000000.00 24.9910000000000 121.470000000000 2 0 100880 1153 1
1433000000.00000 28 619120 6 1433000000000.00 0 0 2 0 100880 103980 24
1433000000.00000 21 619870 6 1433000000000.00 0 0 2 0 100880 1442 24];
exclude=[ 3
4
7
10
17
18
19
28
30
33 ];
Desired output:
1433100000.00000 26 620260 7 1433100000000.00 0 0 2 1 100880 290 23
1433100000.00000 26 620260 7 1433100000000.00 0 0 2 1 100880 290 23
1433100000.00000 25 620160 7 1433100000000.00 0 0 2 1 100880 7274 22
1433100000.00000 21 619910 7 1433100000000.00 24.1190000000000 120.670000000000 2 0 100880 53871 21
1433000000.00000 24 620010 7 1433000000000.00 0 0 2 1 100880 3465 14
1433000000.00000 20 619960 7 1433000000000.00 0 0 2 1 100880 860 11
1433000000.00000 26 619740 6 1433000000000.00 0 0 2 0 100880 52514 8
1433000000.00000 16 619850 6 1433000000000.00 24.9910000000000 121.470000000000 2 0 100880 10505 1
1433000000.00000 16 619880 6 1433000000000.00 24.9910000000000 121.470000000000 2 0 100880 1153 1
1433000000.00000 21 619870 6 1433000000000.00 0 0 2 0 100880 1442 24
Use ismember to find the indices of the second column of user where elements of exclude exist to get the indices of the rows to be removed. Negate these row indices to get the row indices to be kept and use matrix indexing to keep these rows.
user = user(~ismember(user(:,2),exclude),:);

MATLAB initializing variables in next for loop

x=[3 1 1 -5 -2 0 1 2 -2 2];
A=[4 2 6; 0 1 -3; -2 5 -2];
B=[-2 3 2; 1 5 5; -3 1 0];
sum=0;
for i=2:3
sum_j=0;
for j=1:2
sum_j=sum_j+A(1,j)*B(j,i);
end
sum=sum+A(2,i)*sum_j;
end
fprintf('(c) %g\n',sum);
>> (c) -32
-32 is a correct answer. However, if I initialize sum_j=0 outside of the the loop, it returns a different value.
sum=0;
sum_j=0;
for i=2:3
for j=1:2
sum_j=sum_j+A(1,j)*B(j,i);
end
sum=sum+A(2,i)*sum_j;
end
fprintf('(c) %g\n',sum);
>> (c) -98
Can anyone explain why this is happening?
In your first code sum_j gets reinitialized in the loop for i and in second code sum_j carry the value in each loop. Here is a simulation of your codes
first code:
sum = 0
i=2:
sum_j = 0
i=2,j=1
sum_j = 0 + 4*3 = 12
i=2,j=2
sum_j = 12 + 2*5 = 22
sum = 0 + 1*22 = 22
i=3
sum_j = 0
i=3,j=1
sum_j = 0 + 4*2 = 8
i=3,j=2
sum_j = 8 + 2*5 = 18
sum = 22 + -3*18 = -32
Second code
sum = 0
sum_j = 0
i=2:
i=2,j=1
sum_j = 0 + 4*3 = 12
i=2,j=2
sum_j = 12 + 2*5 = 22
sum = 0 + 1*22 = 22
i=3
i=3,j=1
sum_j = 22 + 4*2 = 30
i=3,j=2
sum_j = 30 + 2*5 = 40
sum = 22 + -3*40 = -98

Strange result when converting Hex String to int

I coded the following method to convert a Hex String to a int:
-(long)intFromHexString:(NSString*) string
{
char tempChar;
int temp;
tempChar=[string characterAtIndex:[string length]-1];
temp=strtol(&tempChar, NULL, 16);
NSLog(#"***>%c = %i",tempChar,temp);
return temp;
}
Most of the time it works properly but sometimes it really gets into big trouble like this:
2012-02-10 01:09:28.516 GameView[7664:f803] ***>7 = 7
2012-02-10 01:09:28.517 GameView[7664:f803] ***>7 = 7
2012-02-10 01:09:28.518 GameView[7664:f803] ***>D = 13
2012-02-10 01:09:28.519 GameView[7664:f803] ***>5 = 5
2012-02-10 01:09:28.520 GameView[7664:f803] ***>5 = 5
2012-02-10 01:09:28.520 GameView[7664:f803] ***>D = 13
2012-02-10 01:09:28.521 GameView[7664:f803] ***>4 = 4
2012-02-10 01:09:28.522 GameView[7664:f803] ***>4 = 4
2012-02-10 01:09:28.522 GameView[7664:f803] ***>5 = 5
2012-02-10 01:09:28.523 GameView[7664:f803] ***>4 = 1033 <------this
2012-02-10 01:09:28.524 GameView[7664:f803] ***>C = 12
2012-02-10 01:09:28.524 GameView[7664:f803] ***>B = 11
2012-02-10 01:09:28.525 GameView[7664:f803] ***>3 = 3
2012-02-10 01:09:28.526 GameView[7664:f803] ***>3 = 48 <------this
2012-02-10 01:09:28.527 GameView[7664:f803] ***>B = 11
Can anyone tell me what's wrong with my code?
You are passing a pointer to a single character into strtol(), rather than a NUL-terminated string, so strtol() is sometimes reading beyond the character you gave it. (For instance, "1033" is the result of it finding "409", rather than just "4".)
Fix:
-(long)intFromHexString:(NSString*) string
{
char tempChar[2];
int temp;
tempChar[0]=[string characterAtIndex:[string length]-1];
tempChar[1] = 0;
temp=strtol(tempChar, NULL, 16);
NSLog(#"***>%c = %i",tempChar[0],temp);
return temp;
}