Linear Impulses - Cocos2d/Objective-C/Box2d - iphone

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.

Related

Neural Net with batch size 1 and one input at a time

I tried a very simple autoencoder, 3 inputs, one layer with 2 neurons and the out put with 3.
Just numbers
0.01 0.02 ........... 1.0
0.011 0.021 1.01
0.012 0.022 1.02
That works if all samples, 100, are the input and with 200 epoches.
for (size_t i = 0; i < 100; i++)
{
for (size_t j = 0; j < 3; j++)
{
samples[i][0] = (float)(i+1) * 0.01;
samples[i][1] = (float)(i + 1) * 0.01 + 0.01;
samples[i][2] = (float)(i + 1) * 0.01 + 0.01 + 0.01;
}
}
net.fit<mse>(optimizer, samples, samples, 100, NUM_EPOCHS
, onMinibatch, onEpoch);
But feeding one sample at a time doesn't work.
Like this:
for (int i = 0; i < NUM_EPOCHS; i++)
{
for (int j = 0; j < 100; j++)
{
samples[0][0] = (float)(j + 1) * 0.01;
samples[0][1] = (float)(j + 1) * 0.01 + 0.01;
samples[0][2] = (float)(j + 1) * 0.01 + 0.01 + 0.01;
net.fit<mse>(optimizer, samples, samples, 1, 1
, onMinibatch, onEpoch);
}
}
Is it that bad to feed single samples into a neural network?
Many thanks for your help.
I made a mistake, use
net.train_once<mse>...
does the job.

for loop not behaving correctly in MATLAB

I'm working on a program where I have to quantize a sine wave in MATLAB with 16 quantization levels
I have developed a for loop that should quantize the values properly, and it does for the positive values of the sine wave, but then displays all zeros for the negative values of the signal
Here is the code I have developed:
sig1 = [0 0.6428 0.9848 0.8660 0.3420 -0.3420 -0.8660 -0.9848 -0.6428 -0.0000]
b = 4;
N = 10;
yMax = 1.4088;
yMin = -1.3660;
for i = 1:N
value = ((yMax - yMin)/(2^b));
halfstep = value / 2;
%Calculating quantization levels
value0 = value * 0;
value1 = value * 1;
value2 = value * 2;
value3 = value * 3;
value4 = value * 4;
value5 = value * 5;
value6 = value * 6;
value7 = value * 7;
value8 = value * -1;
value9 = value * -2;
value10 = value * -3;
value11 = value * -4;
value12 = value * -5;
value13 = value * -6;
value14 = value * -7;
value15 = value * -8;
%Quantizing signal1
if value15 < sig1(i) < value14
if sig1(i) < value15 + halfstep
qsig1(i) = -8;
else
qsig1(i) = -7;
end
elseif value14 < sig1(i) < value13
if sig1(i) < value14 + halfstep
qsig1(i) = -7;
else
qsig1(i) = -6;
end
elseif value13 < sig1(i) < value12
if sig1(i) < value13 + halfstep
qsig1(i) = -6;
else
qsig1(i) = -5;
end
elseif value12 < sig1(i) < value11
if sig1(i) < value12 + halfstep
qsig1(i) = -5;
else
qsig1(i) = -4;
end
elseif value11 < sig1(i) < value10
if sig1(i) < value11 + halfstep
qsig1(i) = -4;
else
qsig1(i) = -3;
end
elseif value10 < sig1(i) < value9
if sig1(i) < value10 + halfstep
qsig1(i) = -3;
else
qsig1(i) = -2;
end
elseif value9 < sig1(i) < value8
if sig1(i) < value9 + halfstep
qsig1(i) = -2;
else
qsig1(i) = -1;
end
elseif value8 < sig1(i) < value0
if sig1(i) < value8 + halfstep
qsig1(i) = -1;
else
qsig1(i) = 0;
end
elseif value0 < sig1(i) < value1
if sig1(i) < value0 + halfstep
qsig1(i) = 0;
else
qsig1(i) = 1;
end
elseif value1 < sig1(i) < value2
if sig1(i) < value1 + halfstep
qsig1(i) = 1;
else
qsig1(i) = 2;
end
elseif value2 < sig1(i) < value3
if sig1(i) < value2 + halfstep
qsig1(i) = 2;
else
qsig1(i) = 3;
end
elseif value3 < sig1(i) < value4
if sig1(i) < value3 + halfstep
qsig1(i) = 3;
else
qsig1(i) = 4;
end
elseif value4 < sig1(i) < value5
if sig1(i) < value4 + halfstep
qsig1(i) = 4;
else
qsig1(i) = 5;
end
elseif value5 < sig1(i) < value6
if sig1(i) < value5 + halfstep
qsig1(i) = 5;
else
qsig1(i) = 6;
end
elseif value6 < sig1(i) < value7
if sig1(i) < value6 + halfstep
qsig1(i) = 6;
else
qsig1(i) = 7;
end
elseif sig1(i) < value15
qsig1(i) = 0;
end
end
sig1
qsig1
If anyone can help me figure out why the negative values of sig1 are being made all zeros in qsig1 I would appreciate it!
Thank you!
After more working, I discovered that MATLAB can't do two comparisons as well as I thought it could so I used and statements to achieve the desired result.
Code I used:
if sig1(i) < value15
qsig1(i) = -8;
elseif sig1(i) < value14 && sig1(i) > value15
if value15 < sig1(i) && sig1(i) < (value15 + halfstep)
qsig1(i) = -8;
else
qsig1(i) = -7;
end
elseif sig1(i) < value13 && sig1(i) > value14
if value14 < sig1(i) && sig1(i) < (value14 + halfstep)
qsig1(i) = -7;
else
qsig1(i) = -6;
end
elseif sig1(i) < value12 && sig1(i) > value13
if value13 < sig1(i) && sig1(i) < (value13 + halfstep)
qsig1(i) = -6;
else
qsig1(i) = -5;
end
elseif sig1(i) < value11 && sig1(i) > value12
if value12 < sig1(i) && sig1(i) < (value12 + halfstep)
qsig1(i) = -5;
else
qsig1(i) = -4;
end
elseif sig1(i) < value10 && sig1(i) > value11
if value11 < sig1(i) && sig1(i) < (value11 + halfstep)
qsig1(i) = -4;
else
qsig1(i) = -3;
end
elseif sig1(i) < value9 && sig1(i) > value10
if value10 < sig1(i) && sig1(i) < (value10 + halfstep)
qsig1(i) = -3;
else
qsig1(i) = -2;
end
elseif sig1(i) < value8 && sig1(i) > value9
if value9 < sig1(i) && sig1(i) < (value9 + halfstep)
qsig1(i) = -2;
else
qsig1(i) = -1;
end
elseif sig1(i) < value0 && sig1(i) > value8
if value8 < sig1(i) && sig1(i) < (value8 + halfstep)
qsig1(i) = -1;
else
qsig1(i) = 0;
end
elseif sig1(i) < value1 && sig1(i) > value0
if value0 < sig1(i) && sig1(i) < (value0 + halfstep)
qsig1(i) = 0;
else
qsig1(i) = 1;
end
elseif sig1(i) < value2 && sig1(i) > value1
if value1 < sig1(i) && sig1(i) < (value1 + halfstep)
qsig1(i) = 1;
else
qsig1(i) = 2;
end
elseif sig1(i) < value3 && sig1(i) > value2
if value2 < sig1(i) && sig1(i) < (value2 + halfstep)
qsig1(i) = 2;
else
qsig1(i) = 3;
end
elseif sig1(i) < value4 && sig1(i) > value3
if value3 < sig1(i) && sig1(i) < (value3 + halfstep)
qsig1(i) = 3;
else
qsig1(i) = 4;
end
elseif sig1(i) < value5 && sig1(i) > value4
if value4 < sig1(i) && sig1(i) < (value4 + halfstep)
qsig1(i) = 4;
else
qsig1(i) = 5;
end
elseif sig1(i) < value6 && sig1(i) > value5
if value5 < sig1(i) && sig1(i) < (value5 + halfstep)
qsig1(i) = 5;
else
qsig1(i) = 6;
end
elseif sig1(i) < value7 && sig1(i) > value6
if value6 < sig1(i) && sig1(i) < (value6 + halfstep)
qsig1(i) = 6;
else
qsig1(i) = 7;
end
elseif value7 < sig1(i)
qsig1(i) = 7;
end
Your decision is very over complicated. Try to use function round :
sig1 = [0 0.6428 0.9848 0.8660 0.3420 -0.3420 -0.8660 -0.9848 -0.6428 -0.0000]
b = 4;
N = 10;
yMax = 1.4088;
yMin = -1.3660;
value = ((yMax - yMin)/(2^b));
gsig1=round(sig1./value);

how to select wanted ( needed situations) dual elements which are randomly generated by using random sampling in matlab?

I have 2 different random selected variables. The first one is number of bedrooms which is r1 and the second one is number of people in the dwelling which is r2. there are different certain constant values, which are going to be used for selection of house size.Wanted combinations
There are 24 different combinations that both of random generators can produce by using number of bedrooms and number of people in the house. there is no problem if the random generators produce wanted combinations.If not, there is a problem come out which is unwanted combinations.
How can get rid of this unwanted combinations or how to solve this problem in another way?
My code is as follows:
R1 = randsample('xyzq',1,true,[0.1 0.2 0.43 0.27]); % Probability of number of bedrooms in dwellings
r1=R1;
R2 = randsample('abcdef',1,true,[0.283 0.358 0.163 0.134 0.044 0.018]); %Probability of Household size in UK
r2=R2;
if (r1 == 'x' && r2 == 'a') % 37m2 1 bed, 1 per
A_roof = 37;
A_floor = 37;
A_wall = 35;
A_door = 3;
A_windows = 3;
elseif (r1 == 'x' && r2 == 'b') % 50m2 1 bed, 2 per
A_roof = 50;
A_floor = 50;
A_wall = 47;
A_door = 6;
A_windows = 6;
elseif (r1 == 'y' && r2 == 'c') % 61m2 2 bed, 3 per
A_roof = 61;
A_floor = 61;
A_wall = 42;
A_door = 5;
A_windows = 5;
elseif (r1 == 'y' && r2 == 'd') % 70m2 2 bed, 4 per
A_roof = 70;
A_floor = 70;
A_wall = 50;
A_door = 5;
A_windows = 5;
elseif (r1 == 'y' && r2 == 'd') % 74m2 3 bed, 4 per
A_roof = 74;
A_floor = 74;
A_wall = 51;
A_door = 6;
A_windows = 6;
elseif (r1 == 'z' && r2 == 'e') % 86m2 3 bed, 5 per
A_roof = 86;
A_floor = 86;
A_wall = 55;
A_door = 6;
A_windows = 6;
elseif (r1 == 'z' && r2 == 'f') % 95m2 3 bed, 6 per
A_roof = 95;
A_floor = 95;
A_wall = 70;
A_door = 7;
A_windows = 7;
elseif (r1 == 'q' && r2 == 'e') % 90m2 4 bed, 5 per
A_roof = 90;
A_floor = 90;
A_wall = 68;
A_door = 7;
A_windows = 7;
elseif (r1 == 'q' && r2 == 'f') % 99m2 4 bed, 6 per
A_roof = 99;
A_floor = 99;
A_wall = 74;
A_door = 8;
A_windows = 8;
elseif (r1 == 'y' && r2 == 'd') % 83m2 2 bed, 4 per
A_roof = 40;
A_floor = 83;
A_wall = 105;
A_door = 8;
A_windows = 8;
elseif (r1 == 'z') && (r2 == 'd') % 87m2 3 bed, 4 per
A_roof = 42;
A_floor = 87;
A_wall = 105;
A_door = 8;
A_windows = 8;
elseif (r1 == 'z' && r2 == 'e') % 96m2 3 bed, 5 per
A_roof = 46;
A_floor = 96;
A_wall = 150;
A_door = 10;
A_windows = 10;
elseif (r1 == 'q' && r2 == 'e') % 100m2 4 bed, 5 per
A_roof = 50;
A_floor = 100;
A_wall = 180;
A_door = 10;
A_windows = 10;
elseif(r1 == 'q' && r2 == 'f') % 107m2 4 bed, 6 per
A_roof = 55;
A_floor = 107;
A_wall = 125;
A_door = 10;
A_windows = 10;
elseif (r1 == 'z' && r2 == 'e') % 102m2 3 bed, 5 per
A_roof = 50;
A_floor = 102;
A_wall = 200;
A_door = 10;
A_windows = 10;
elseif (r1 == 'q' && r2 == 'e') % 106m2 4 bed, 5 per
A_roof = 55;
A_floor = 106;
A_wall = 200;
A_door = 10;
A_windows = 10;
elseif (r1 == 'q' && r2 == 'f') % 113m2 4 bed, 6 per
A_roof = 60;
A_floor = 113;
A_wall = 200;
A_door = 10;
A_windows = 10;
end

VHDL PS/2 interface

I am using VHDL and an FPGA board, a VGA interface and a PS/2 keyboard interface to create a maze and make a square that can move through the maze. When I press one of the keys (WASD), the square moves only one position and then will not move again. I need it to move every time one of the keys is pressed. Here is my code:
ENTITY hw_image_generator IS
PORT(
ps2_code : IN STD_LOGIC_VECTOR(7 DOWNTO 0);
game_clk : IN STD_LOGIC;
disp_ena : IN STD_LOGIC; --display enable ('1' = display time, '0' = blanking time)
row : IN INTEGER; --row pixel coordinate
column : IN INTEGER; --column pixel coordinate
red : OUT STD_LOGIC_VECTOR(7 DOWNTO 0) := (OTHERS => '0'); --red magnitude output to DAC
green : OUT STD_LOGIC_VECTOR(7 DOWNTO 0) := (OTHERS => '0'); --green magnitude output to DAC
blue : OUT STD_LOGIC_VECTOR(7 DOWNTO 0) := (OTHERS => '0')); --blue magnitude output to DAC
END hw_image_generator;
ARCHITECTURE behavior OF hw_image_generator IS
signal x_position: INTEGER := 20;
signal y_position: INTEGER := 20;
CONSTANT max_count: NATURAL := 500000;
SIGNAL reset: STD_LOGIC;
BEGIN
PROCESS(game_clk, reset, x_position, y_position)
VARIABLE count : NATURAL range 0 to max_count;
BEGIN
IF (reset = '1') THEN
count := 0;
x_position <= 20;
y_position <= 20;
ELSIF(rising_edge(game_clk) AND count = 0) THEN
IF(count < max_count)THEN
IF(ps2_code = "00101001") THEN -- space bar
count := 0;
x_position <= 20;
y_position <= 20;
END IF;
IF(ps2_code = "00011101") THEN --W key
count := count + 1;
y_position <= y_position;
x_position <= x_position - 10;
END IF;
IF(ps2_code = "00011011") THEN --S key
count := count + 1;
y_position <= y_position;
x_position <= x_position + 10;
END IF;
IF(ps2_code = "00100011") THEN --D key
count := count + 1;
x_position <= x_position;
y_position <= y_position + 10;
END IF;
IF(ps2_code = "00011100") THEN --A key
count := count + 1;
x_position <= x_position;
y_position <= y_position - 10;
END IF;
ELSE
count := 0;
END IF;
END IF;
END PROCESS;
PROCESS(disp_ena, row, column)
BEGIN
IF(disp_ena = '1') THEN --display time
IF(row < 512 AND column > 231 AND column < 281) THEN
red <= (OTHERS => '0');
green <= (OTHERS => '0');
blue <= (OTHERS => '0');
ELSIF(row > 743 AND row < 793 AND column < 512) THEN
red <= (OTHERS => '0');
green <= (OTHERS => '0');
blue <= (OTHERS => '0');
ELSIF(row > 1024 AND column > 231 AND column < 281) THEN
red <= (OTHERS => '0');
green <= (OTHERS => '0');
blue <= (OTHERS => '0');
ELSIF(row > 256 AND row < 1024 AND column > 487 AND column < 537) THEN
red <= (OTHERS => '0');
green <= (OTHERS => '0');
blue <= (OTHERS => '0');
ELSIF(row > 487 AND row < 537 AND column > 536 AND column < 768) THEN
red <= (OTHERS => '0');
green <= (OTHERS => '0');
blue <= (OTHERS => '0');
ELSIF(row > 231 AND row < 281 AND column > 768) THEN
red <= (OTHERS => '0');
green <= (OTHERS => '0');
blue <= (OTHERS => '0');
ELSIF(row > 743 AND row < 793 AND column > 768) THEN
red <= (OTHERS => '0');
green <= (OTHERS => '0');
blue <= (OTHERS => '0');
ELSIF(row > 974 AND row < 1024 AND column > 536 AND column < 768) THEN
red <= (OTHERS => '0');
green <= (OTHERS => '0');
blue <= (OTHERS => '0');
ELSE
red <= (OTHERS => '1');
green <= (OTHERS => '1');
blue <= (OTHERS => '1');
END IF;
IF(row > x_position AND row < x_position+50 AND column > y_position AND column < y_position+50) THEN
red <= (OTHERS => '1');
green <= (OTHERS => '0');
blue <= (OTHERS => '0');
END IF;
ELSE --blanking time
red <= (OTHERS => '0');
green <= (OTHERS => '0');
blue <= (OTHERS => '0');
END IF;
END PROCESS;
END behavior;
In your unlabelled process sensitive to game_clk:
process (game_clk, reset, x_position, y_position)
variable count: natural range 0 to max_count;
begin
if reset = '1' then
count := 0;
x_position <= 20;
y_position <= 20;
elsif rising_edge(game_clk) and count = 0 then
if count < max_count then
if ps2_code = "00101001" then -- space bar
count := 0;
x_position <= 20;
y_position <= 20;
end if;
if ps2_code = "00011101" then --w key
count := count + 1;
y_position <= y_position;
x_position <= x_position - 10;
end if;
if ps2_code = "00011011" then --s key
count := count + 1;
y_position <= y_position;
x_position <= x_position + 10;
end if;
if ps2_code = "00100011" then --d key
count := count + 1;
x_position <= x_position;
y_position <= y_position + 10;
end if;
if ps2_code = "00011100" then --a key
count := count + 1;
x_position <= x_position;
y_position <= y_position - 10;
end if;
else
count := 0;
end if;
end if;
end process;
Notice you don't need x_position and y_position in the sensitivity list. All signal assignments are either due to a reset event or a game_clk event.
Also note the enable count = 0 in the condition for the elsif:
elsif rising_edge(game_clk) and count = 0 then
And the condition in the following if statement:
if count < max_count then
Once any of your ps2_code recognizers (except the space bar) is valid you'll increment count and no longer enable any flip flops and registers you're assigning here on the rising edge of game_clk. You also can't tell if you hit successive space bars. without hitting another recognizer.
Your code matches your description of your problem, it's a feature.
PROCESS(game_clk)
BEGIN
IF(game_clk'EVENT AND game_clk = '1')THEN
count <= count + 1;
IF(count = 5000000 AND ps2_code = "00101001")THEN --space key
count <= 0;
x_position <= 20;
y_position <= 20;
END IF;
IF(count = 5000000 AND ps2_code = "00011101")THEN --W key
count <= 0;
IF((y_position > 0 AND y_position < 181 AND x_position > 0 and x_position < 512) --1
OR (y_position > 0 AND y_position < 437 AND x_position > 512 AND x_position < 693) --2
OR (y_position > 281 AND y_position < 437 AND x_position > 0 and x_position < 512) --3
OR (y_position > 437 AND y_position < 537 AND x_position > 0 AND x_position < 206) --4
OR (y_position > 537 AND y_position < 1227 AND x_position > 0 AND x_position < 181) --5
OR (y_position > 537 AND y_position < 718 AND x_position > 181 AND x_position < 437) --6
OR (y_position > 768 AND y_position < 1227 AND x_position > 281 AND x_position < 718) --7
OR (y_position > 537 AND y_position < 768 AND x_position > 537 AND x_position < 693) --8
OR (y_position > 537 AND y_position < 718 AND x_position > 693 AND x_position < 793) --9
OR (y_position > 537 AND y_position < 768 AND x_position > 793 AND x_position < 975) --10
OR (y_position > 768 AND y_position < 1227 AND x_position > 793 AND x_position < 1027) --11
OR (y_position > 718 AND y_position < 768 AND x_position > 281 AND x_position < 437) --12
) THEN
x_position <= x_position - 10;
ELSE
x_position <= x_position + 5;
y_position <= y_position;
END IF;
ELSIF(count = 5000000 AND ps2_code = "00011011") THEN --S key
count <= 0;
IF((y_position > 0 AND y_position < 181 AND x_position > 0 and x_position < 512) --1
OR (y_position > 0 AND y_position < 437 AND x_position > 512 AND x_position < 693) --2
OR (y_position > 281 AND y_position < 437 AND x_position > 0 and x_position < 512) --3
OR (y_position > 437 AND y_position < 537 AND x_position > 0 AND x_position < 206) --4
OR (y_position > 537 AND y_position < 1227 AND x_position > 0 AND x_position < 181) --5
OR (y_position > 537 AND y_position < 718 AND x_position > 181 AND x_position < 437) --6
OR (y_position > 768 AND y_position < 1227 AND x_position > 281 AND x_position < 718) --7
OR (y_position > 537 AND y_position < 768 AND x_position > 537 AND x_position < 693) --8
OR (y_position > 537 AND y_position < 718 AND x_position > 693 AND x_position < 793) --9
OR (y_position > 537 AND y_position < 768 AND x_position > 793 AND x_position < 975) --10
OR (y_position > 768 AND y_position < 1227 AND x_position > 793 AND x_position < 1027) --11
OR (y_position > 718 AND y_position < 768 AND x_position > 281 AND x_position < 437) --12
) THEN
x_position <= x_position + 10;
ELSE
x_position <= x_position - 5;
y_position <= y_position;
END IF;
ELSIF(count = 5000000 AND ps2_code = "00100011") THEN --D key
count <= 0;
IF((y_position > 0 AND y_position < 181 AND x_position > 0 and x_position < 512) --1
OR (y_position > 0 AND y_position < 437 AND x_position > 512 AND x_position < 693) --2
OR (y_position > 281 AND y_position < 437 AND x_position > 0 and x_position < 512) --3
OR (y_position > 437 AND y_position < 537 AND x_position > 0 AND x_position < 206) --4
OR (y_position > 537 AND y_position < 1227 AND x_position > 0 AND x_position < 181) --5
OR (y_position > 537 AND y_position < 718 AND x_position > 181 AND x_position < 437) --6
OR (y_position > 768 AND y_position < 1227 AND x_position > 281 AND x_position < 718) --7
OR (y_position > 537 AND y_position < 768 AND x_position > 537 AND x_position < 693) --8
OR (y_position > 537 AND y_position < 718 AND x_position > 693 AND x_position < 793) --9
OR (y_position > 537 AND y_position < 768 AND x_position > 793 AND x_position < 975) --10
OR (y_position > 768 AND y_position < 1227 AND x_position > 793 AND x_position < 1027) --11
OR (y_position > 718 AND y_position < 768 AND x_position > 281 AND x_position < 437) --12
) THEN
y_position <= y_position + 10;
ELSE
x_position <= x_position;
y_position <= y_position -5;
END IF;
ELSIF(count = 5000000 AND ps2_code = "00011100") THEN --A key
count <= 0;
IF((y_position > 0 AND y_position < 181 AND x_position > 0 and x_position < 512) --1
OR (y_position > 0 AND y_position < 437 AND x_position > 512 AND x_position < 693) --2
OR (y_position > 281 AND y_position < 437 AND x_position > 0 and x_position < 512) --3
OR (y_position > 437 AND y_position < 537 AND x_position > 0 AND x_position < 206) --4
OR (y_position > 537 AND y_position < 1227 AND x_position > 0 AND x_position < 181) --5
OR (y_position > 537 AND y_position < 718 AND x_position > 181 AND x_position < 437) --6
OR (y_position > 768 AND y_position < 1227 AND x_position > 281 AND x_position < 718) --7
OR (y_position > 537 AND y_position < 768 AND x_position > 537 AND x_position < 693) --8
OR (y_position > 537 AND y_position < 718 AND x_position > 693 AND x_position < 793) --9
OR (y_position > 537 AND y_position < 768 AND x_position > 793 AND x_position < 975) --10
OR (y_position > 768 AND y_position < 1227 AND x_position > 793 AND x_position < 1027) --11
OR (y_position > 718 AND y_position < 768 AND x_position > 281 AND x_position < 437) --12
) THEN
y_position <= y_position - 10;
ELSE
x_position <= x_position;
y_position <= y_position +5;
END IF;
END IF;
END IF;
END PROCESS;

How to specify a range and perform a function accordingly in matlab?

I need to perform the following function in matlab.
I had tried the following code but somehow my if statement is wrong. I'd like to know how to use the if statement efficiently here. If there is any other method in which i could perform the function please do help. My code is as follows
if (y(i,j) < -0.5, y(i,j) >= -1)
f(i,j) = 0
elseif (y(i,j) < 0, y(i,j) >= -0.5)
f(i,j) = 1
elseif (y(i,j) < 0.75, y(i,j) >= 0)
f(i,j) = 2
elseif (y(i,j) < 1, y(i,j) >= 0.75)
f(i,j) = 3
end
Here y(i,j) is a 1 x 256 matrix. Thanks
You need to use the logical AND operator to tie two Boolean expressions together. You are using a comma which is not correct:
if (y(i,j) < -0.5 && y(i,j) >= -1)
f(i,j) = 0
elseif (y(i,j) < 0 && y(i,j) >= -0.5)
f(i,j) = 1
elseif (y(i,j) < 0.75 && y(i,j) >= 0)
f(i,j) = 2
elseif (y(i,j) < 1 && y(i,j) >= 0.75)
f(i,j) = 3
end
However, it looks like you're using this in a for loop and I wouldn't perform the above in a loop. Use logical indexing instead:
f(y < -0.5 & y >= 1) = 0;
f(y < 0 & y >= -0.5) = 1;
f(y < 0.75 & y >= 0) = 2;
f(y < 1 & y >= 0.75) = 3;
This is assuming that f is the same size as y.