lundi 3 septembre 2018

Ratios in matrix | Matrix/Vector | if statement/loop |

I was wondering how would you explain below function to work with matrix instead of vector input, essentially I just find all values that is over a certain value in that column and add it up and divide it by total rows to give me ratio. Below is working for vector input but I kinda not sure how to complete the one with matrix input. Should I use logical instead?

The method I chosen don't think will work if there is more than 2 columns but I wish it could accommodate any size.

function Ratio = ratiovector(Vector)

N = numel(Vector);
c=0

for a = 1:N

    if Vectors(a) >= 20
        c=c + 1;
    end
    end


Ratio = c/N;
end

Matrix Input

function Ratio = ratiovector2(Matrix)

rows = Matrix(:,1)
columns = Matrix(:,2)
c = 0
d = 0

for a = 1:rows
    for b =1:columns

    if Matrix(a) >= 20
        c= c + 1;
    if Matrix(b) >= 20
        d= d + 1;
    end
    end


Ratio = ?;
end

python seem to ignore part of my if statement

I have an if statement which has 3 requirements.

if  (-30 < distancey1_1 < 30) and (german_startx1 < 705) and (distance1_1 < 600) :               # is german 1 in position and are the russians in range (between 30 and minus 30 doesnt work)
    print(distancey1_1)
    print('1_shoots_1')
    shoot(bulletx1, bullety1, 10, 10, yellow)

Somehow it runs this if statement, even when all 3 requirements are not met.

i get this as an output sometimes. its basically admitting that its ignoring the -30 < distancey1_1 < 30 rule

1 shoots 1
-152.0
1 shoots 1
-152.0
1 shoots 1
-152.0
1 shoots 1
-152.0
1 shoots 1

this is part of a stupid little recreation im making of that scene from enemy at the gates.

the full code is below. it just makes squares which moves side ways, some of them shoot.

import pygame
import time
import random
import math

pygame.init()

display_width = 800
display_height= 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
grey=(169,169,169)
yellow = (246, 240, 55)

gameDisplay= pygame.display.set_mode((800,600))

pygame.display.set_caption('stalingrad V0.1')

clock = pygame.time.Clock()



def spawn_soldier(thingx,thingy, thingw, thingh, colour):
    pygame.draw.rect(gameDisplay, colour,[thingx, thingy, thingw, thingh])

def shoot(bulletx, bullety, bulletw, bulleth, colour):
    pygame.draw.rect(gameDisplay, colour,[bulletx, bullety, bulletw, bulleth])



def game_loop():

# soldier constants

russian_width= 20
russian_height= 20
russian_speed = 6


russian_startx1=-300
russian_startx2=-310
russian_startx3=-322
russian_startx4=-305
russian_startx5=-306
russian_startx6=-313


russian_starty1 =display_height/2
russian_starty2 =random.randrange(0, display_height)
russian_starty3 =random.randrange(0, display_height)
russian_starty4 =random.randrange(0, display_height)
russian_starty5 =random.randrange(0, display_height)




german_width=20
german_height=20
german_speed=-1

german_startx1=780
german_startx2=820
german_startx3=789
german_startx4=835
german_startx5=780
german_startx6=820
german_startx7=789
german_startx8=835





german_start1y=display_height/2
german_start2y=random.randrange(0, display_height)
german_start3y=random.randrange(0, display_height)
german_start4y=random.randrange(0, display_height)
german_start5y=random.randrange(0, display_height)
german_start6y=random.randrange(0, display_height)
german_start7y=random.randrange(0, display_height)
german_start8y=random.randrange(0, display_height)
german_start9y=random.randrange(0, display_height)
german_start10y=random.randrange(0, display_height)
german_start11y=random.randrange(0, display_height)
german_start12y=random.randrange(0, display_height)

bulletx1 = german_startx1 - 5
bullety1 = german_start1y - 5


germancount=0
russiancount=0
bullet_speed=-10

game_exit=False

soldier1=1

while not game_exit:
    gameDisplay.fill(white)

    ###
    #below = spawn germans
    ###

    spawn_soldier(german_startx1, german_start1y, german_width, german_height, grey)   # gerry 1

    spawn_soldier(german_startx2, german_start2y, german_width, german_height, grey)   # gerry 2

    spawn_soldier(german_startx3, german_start3y, german_width, german_height, grey)   # gerry 3

    spawn_soldier(german_startx4, german_start4y, german_width, german_height, grey)   # gerry 4


    ###
    # below = move germans into position
    ###


    if german_startx1 > 700:
        german_startx1 += german_speed

    if german_startx2 > 700:
        german_startx2 += german_speed

    if german_startx3 > 700:
        german_startx3 += german_speed

    if german_startx4 > 700:
        german_startx4 += german_speed


        #####
        # below = spawn russians and move them right
        ####


    spawn_soldier(russian_startx1, russian_starty1, russian_width, russian_height, red) # rusky 1
    if russian_startx1 < display_width+90:       #russian moves to the right until off screen
        russian_startx1 += russian_speed         
    if russian_startx1 > 810:       # once its gone beyond the screen give me a fresh russian from the left    
        russian_starty1 = random.randrange(0, display_height)
        russian_startx1 = -30
        print('reset rusky 1')

    spawn_soldier(russian_startx2, russian_starty2, russian_width, russian_height, red) # rusky 2
    if russian_startx2 < display_width+90:       
        russian_startx2 += russian_speed         
    if russian_startx2 > 830:       
        russian_starty2 = random.randrange(0, display_height)
        russian_startx2 = -30
        print('reset rusky 2')

        ###
        # below = distance between each russian and each german. if touching german dies and respawns
        ###

    distancex1_1 = russian_startx1 - german_startx1
    distancey1_1 = russian_starty1 - german_start1y
    distance1_1 = math.sqrt((distancex1_1)**2+(distancey1_1)**2)
    if distance1_1 < 10:
        german_startx1= 780

    distancex1_2 = russian_startx1 - german_startx2
    distancey1_2 = russian_starty1 - german_start2y
    distance1_2 = math.sqrt((distancex1_2)**2+(distancey1_2)**2)
    if distance1_2 < 10:
        german_startx2= 820

    distancex1_3 = russian_startx1 - german_startx3
    distancey1_3 = russian_starty1 - german_start3y
    distance1_3 = math.sqrt((distancex1_3)**2+(distancey1_3)**2)
    if distance1_3 < 10:
        german_startx3= 789

    distancex1_4 = russian_startx1 - german_startx4
    distancey1_4 = russian_starty1 - german_start4y
    distance1_4 = math.sqrt((distancex1_4)**2+(distancey1_4)**2)
    if distance1_4 < 10:
        german_startx4= 780

    distancex1_5 = russian_startx1 - german_startx5
    distancey1_5 = russian_starty1 - german_start5y
    distance1_5 = math.sqrt((distancex1_5)**2+(distancey1_5)**2)
    if distance1_5 < 10:
        german_startx5= 7999

    distancex1_6 = russian_startx1 - german_startx6
    distancey1_6 = russian_starty1 - german_start6y
    distance1_6 = math.sqrt((distancex1_6)**2+(distancey1_6)**2)
    if distance1_6 < 10:
        german_startx6= 789

    distancex2_1 = russian_startx2 - german_startx1
    distancey2_1 = russian_starty2 - german_start1y
    distance2_1 = math.sqrt((distancex2_1)**2+(distancey2_1)**2)
    if distance2_1 < 10:
        german_startx1= 780

    distancex2_2 = russian_startx2 - german_startx2
    distancey2_2 = russian_starty2 - german_start2y
    distance2_2 = math.sqrt((distancex2_2)**2+(distancey2_2)**2)
    if distance2_2 < 10:
        german_startx2= 820

    distancex2_3 = russian_startx2 - german_startx3
    distancey2_3 = russian_starty2 - german_start3y
    distance2_3 = math.sqrt((distancex2_3)**2+(distancey2_3)**2)
    if distance2_3 < 10:
        german_startx3= 789

    distancex2_4 = russian_startx2 - german_startx4
    distancey2_4 = russian_starty2 - german_start4y
    distance2_4 = math.sqrt((distancex2_4)**2+(distancey2_4)**2)
    if distance2_4 < 10:
        german_startx4= 780

    distancex2_5 = russian_startx2 - german_startx5
    distancey2_5 = russian_starty2 - german_start5y
    distance2_5 = math.sqrt((distancex2_5)**2+(distancey2_5)**2)
    if distance2_5 < 10:
        german_startx5= 7999

    distancex1_6 = russian_startx1 - german_startx6
    distancey1_6 = russian_starty1 - german_start6y
    distance1_6 = math.sqrt((distancex1_6)**2+(distancey1_6)**2)

    if distance1_6 < 10:
        german_startx6= 789

        ###
        # below = shooting
        ###

    print(distancey1_1)

    if  (-30 < distancey1_1 < 30) and (german_startx1 < 705) and (distance1_1 < 600) :               # is german 1 in position and are the russians in range (between 30 and minus 30 doesnt work)
        print(distancey1_1)
        print('1_shoots_1')
        shoot(bulletx1, bullety1, 10, 10, yellow)

        bulletx1 += bullet_speed
        if bulletx1 < -100:                                                               #firing again
            bulletx1 = german_startx1 -5
        if distancey1_1 > 0:                                                              # vertical aiming
            print('aim up')
            bullety1 += 1
        if distancey1_1< 0:
            bullety1 += -1
            print('aim down')

        disx1_1 = russian_startx1 - bulletx1                                                #distance between bullet and target
        disy1_1 = russian_starty1 - bullety1
        if -10< disx1_1 < 10 and -10< disy1_1 <10:
            bulletx1 =-10
            russian_startx1 = -30
            russian_starty1 =random.randrange(0, display_height)




#####change numbers
    if german_startx1 < 705 and distance1_2 < 600 and  distancey1_2 <30 :               # is german 1 in position and are the russians in range

        print('1 shoots 1')
        shoot(bulletx1, german_start1y, 10, 10, yellow)

        bulletx1 += bullet_speed
        if bulletx1 < 100:
            bulletx1 = german_startx1 - 5










    pygame.display.update()
    clock.tick(40)


game_loop()
pygame.quit()
quit()

how can i make it only perform the if statement when all 3 things are met?

Issue comparing values in php

I have this php code meant to compare two values,a variable $rate received from a form which can either have values 'applaud' or 'boo' so I want to check if the value is neither of it and kill the page with an error message.I've tried that but ...localhost can not handle this request

HERE IS MY CODE:

<?php 
$rate=$_POST['rate'];
echo $rate;

?>

<?php

if($rate != 'applaud' OR $rate != 'boo')
{

die("Sorry there was a problem var rate was not well stated");
}

else
{
    echo 'yay ,well stated!!!';
}
?>

Excel select first value belong to one name

I need get first value which belong to one fiscal week and the others have zero. I have IF(COUNTIF(B2:B40,B2)>1,IF(COUNTIF(B$2:B2,B2)=1,C2,0),""). But I also want the same in the same cell for another person.

A B C D E An Khaly August Week 32 40 40 40 An Khaly August Week 32 40 0 0 An Khaly August Week 32 40 0 0 An Khaly August Week 32 40 0 0 An Khaly August Week 32 40 0 0 An Khaly August Week 33 40 40 40 An Khaly August Week 34 40 40 40 An Khaly August Week 34 40 0 0 An Khaly August Week 34 40 0 0 Zita Pork August Week 31 40 40 40 Zita Pork August Week 31 40 0 0 Zita Pork August Week 31 40 0 0 Zita Pork August Week 31 40 0 0 Zita Pork August Week 31 40 0 0 Zita Pork August Week 32 20 0 20 Zita Pork August Week 32 20 0 0 Zita Pork August Week 32 20 0 0 Zita Pork August Week 33 20 0 20 Zita Pork August Week 33 20 0 0

My problem is, that my formula works only for one person (column D) but I need to work for all name with the same fiscal week (column E). Can anybody please help? Thx

Why does php give me syntax error for this valid if...else statement [duplicate]

This question already has an answer here:

So I have this code but I keep getting syntax error saying :unexpected '=' in line 9.I don't understand..I've tried what i can.

<?php 
$rate=$_POST['rate'];
echo $rate;

?>

<?php

if($rate !=== 'applaud' OR $rate !=== 'applaud')
{

die("Sorry there was a problem var rate was not well stated");
}

else
{
    echo 'yay ,well stated!!!';
}
?>



?>

Google charts draw becomes very slow in If clause

I have a Google column chart which is working just fine, although in some cases it could happen that the data set returned from the SalesQueryN.asp is empty. To avoid an error message being displayed, I tried to use an

if (condition) {} else {} 

clause to check if the data set is empty, otherwise it should draw the chart.

Without the if, the chart is drawn instantly, but this line

chart.draw(data, google.charts.Bar.convertOptions(options));

is within the else {} clause, the browser hangs for some 30 seconds. Eventually it does display part of the chart, but the performance is not acceptable, and it doesn't display the proper full chart.

Here is the Ajax code that I use to draw the chart.

    google.charts.setOnLoadCallback(function() { drawsdr(cYear,cMonth,'CST')});

    function drawsdr(rY, rM, rS) {
      if (rY === undefined) {
          rY = Date().getFullYear();
      }
      if (rM === undefined) {
          rM = Date().getMonth();
      }
      if (rS === undefined) {
          rS = 'CST';
      }
      $.ajax({
          url: "api/SalesQueryN.asp?P=T&Y=" + rY + "&M=" + rM + "&S=" + rS,
          dataType: "json",
          async: true,
          success: function (jsonData) {
             var data = new google.visualization.arrayToDataTable(jsonData);
             var chart = new google.charts.Bar(document.getElementById('s'+rS));
             var options = { 
                 title: rY + '-' + rM,
                 legend: { position: 'bottom' },
                 bar: { groupWidth: '80%' },
                 fontSize: xFS, fontName: xFo,
                 isStacked: false,
                 seriesType: 'bars',
                 vAxis: { format: '#,###' },
                 hAxis: { textStyle: { fontSize: xFS } }
             };
             if (data.getColumnLabel(0) == "empty") {
                document.getElementById("sCST").innerHTML = "<p>There is no data to display for " + rY + "-" + rM + "</p>";
             } else {
                chart.draw(data, google.charts.Bar.convertOptions(options));
             }
          },
          error: function (response) {
             alert(Object.keys(response));
             //alert(response.statusText);
             alert(response.responseText);
          }
        });
      }

I'm not quite sure why the if clause is affecting the performance so much.

My question now is how can I make the check on the data table without slowing down the chart's drawing?

PHP inline if statement always returns false

can't seem to start this working properly, any idea what could be the problem? The variable APP_NAME is equal to string 'test', if I do this if statement in a regular way it works as it should.

<?=form_radio('filter', 'y', $info['enable_categories'] == 'y' ? true : false, 'id="app" '.APP_NAME == "test" ? '' : 'disabled'.'')?>

The disabled parameter has to be put into the same last parameter as id.

But this way I get the false value even if APP_NAME equals 'test'.