for loop not behaving correctly in MATLAB - 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);

Related

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

MATLAB: Error using vertcat. Dimensions of matrices being concatenated are not consistent

I have two tables containing strings that I want to concatenate. The strings will vary in length from concatenation to concatenation. I have checked that all variables match/dimensions match.
That is the baffling part: I have two tables of equal dimensions with equal variables, but I will still get the error listed in the title. In a nutshell: has this sort of error ever happened before or is there something else going on with my code (rather, is this sort of error possible)? If this has happened before, what were some fixes? Again: I have verified multiple times that the dimensions of the two tables do agree.
Top-Level Loop that calls the concatenation functions.
function saveAnnotation(obj)
switch obj.annotationMode
case 1
isCellEmpty = isFieldEmpty(obj);
if isCellEmpty
createNewAnnoTable(obj);
elseif ~isCellEmpty
addToAnnoTable(obj);
end
set(obj.regionSelector, 'Enable', 'off');
case 2
isCellEmpty = isFieldEmpty(obj);
if isCellEmpty
createNewAnnoTable(obj);
elseif ~isCellEmpty
addToAnnoTable(obj);
end
case 3
if obj.selectedLead == 1
for i = 1:6
fillRecPairInfo(obj, i);
isCellEmpty = isFieldEmpty(obj, i);
if isCellEmpty
createNewAnnoTable(obj, i);
elseif ~isCellEmpty
addToAnnoTable(obj, i);
end
end
elseif obj.selectedLead == 2
for i = 7:12
fillRecPairInfo(obj, i);
isCellEmpty = isFieldEmpty(obj, i);
if isCellEmpty
createNewAnnoTable(obj, i);
elseif ~isCellEmpty
addToAnnoTable(obj, i);
end
end
end
case 4
for i = 1:12
fillRecPairInfo(obj, i);
isCellEmpty = isFieldEmpty(obj, i);
if isCellEmpty
createNewAnnoTable(obj, i);
elseif ~isCellEmpty
addToAnnoTable(obj, i);
end
end
end
end
The "isFieldEmpty" function
function fieldEmpty = isFieldEmpty(obj, varargin)
pairSelection = obj.selectedPair;
displayedPair = find(pairSelection(1:12) == 1);
switch obj.annotationMode
case 1
for i = 1:length(displayedPair)
if displayedPair(i) <= 6
fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData);
elseif displayedPair(i) > 6
fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData);
end
end
case 2
for i = 1:length(displayedPair)
if displayedPair(i) <= 6
fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData);
elseif displayedPair(i) > 6
fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData);
end
end
case 3
checkIndx = varargin{1};
if checkIndx <= 6
fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData);
elseif checkIndx > 6
fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData);
end
case 4
checkIndx = varargin{1};
if checkIndx <= 6
fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData);
elseif checkIndx > 6
fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData);
end
end
end
The "createNewAnnoTable" function
function createNewAnnoTable(obj, varargin)
pairSelection = obj.selectedPair;
displayedPair = find(pairSelection(1:12) == 1);
switch obj.annotationMode
case 1
for i = 1:length(displayedPair)
if displayedPair(i) <= 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations = obj.nextRow;
elseif displayedPair(i) > 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations = obj.nextRow;
end
end
case 2
for i = 1:length(displayedPair)
if displayedPair(i) <= 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations = obj.nextRow;
elseif displayedPair(i) > 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations = obj.nextRow;
end
end
case 3
checkIndx = varargin{1};
if checkIndx <= 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations = obj.nextRow;
elseif checkIndx > 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations = obj.nextRow;
end
case 4
checkIndx = varargin{1};
if checkIndx <= 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations = obj.nextRow;
elseif checkIndx > 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations = obj.nextRow;
end
end
end
The "addToAnnoTable" function
function addToAnnoTable(obj, varargin)
switch obj.annotationMode
case 1
pairSelection = obj.selectedPair;
displayedPair = find(pairSelection(1:12) == 1);
for i = 1:length(displayedPair)
if displayedPair(i) <= 6
existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations;
elseif displayedPair(i) > 6
existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations;
end
end
for i = 1:length(displayedPair)
if displayedPair(i) <= 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations = [existingTable; obj.nextRow];
elseif displayedPair(i) > 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations = [existingTable; obj.nextRow];
end
end
case 2
pairSelection = obj.selectedPair;
displayedPair = find(pairSelection(1:12) == 1);
for i = 1:length(displayedPair)
if displayedPair(i) <= 6
existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations;
elseif displayedPair(i) > 6
existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations;
end
end
for i = 1:length(displayedPair)
if displayedPair(i) <= 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations = [existingTable; obj.nextRow];
elseif displayedPair(i) > 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations = [existingTable; obj.nextRow];
end
end
case 3
checkIndx = varargin{1};
if checkIndx <= 6
existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations;
elseif checkIndx > 6
existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations;
end
if checkIndx <= 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations = [existingTable; obj.nextRow];
elseif checkIndx > 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations = [existingTable; obj.nextRow];
end
case 4
checkIndx = varargin{1};
if checkIndx <= 6
existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations;
elseif checkIndx > 6
existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations;
end
if checkIndx <= 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations = [existingTable; obj.nextRow];
elseif checkIndx > 6
obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations = [existingTable; obj.nextRow];
end
end
end
After further research I realized that my table didn't contain exclusively strings. It actually contained a combination of stand-alone strings and string-cells. When MATLAB would try to concatenate the stand-alone strings, that is when the error would occur.
I suppose the issue is that MATLAB handles strings like a vector of characters not a single object so putting it in a single column in a table can cause concatenation problems when the new string is a different length.
I was able to fix this problem by making every stand-alone string a string-cell. That has fixed my problem because now MATLAB is concatenating cells rather than strings.

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.

MATLAB Figure file

I have a problem with contour figure, when I run my syntax I get error like:
Error using contourf (line 66)
Z must be size 2x2 or greater.
Error in example (line 660)
contourf (x,y,c); colorbar;
and this is my syntax:
%---------------------stack1--------------------------------------------
v = 0.5; % velocity
lambda = 0; % decay rate
Q = 1; % emission rate(s)
u = 3; % wind speed
P = 1; % Ambient Pressure
D = 6; % inside diameter
V = 22.4; % volumetric flow rate of stack gas
Ts = 400; % temperature of stack gas
Ta = 283; % temperature of ambient air
xstack = 0; ystack = 60; % stack location(s)
xmin = 0.5; xmax = 3.5; % x-axis interval
ymin = 0; ymax = 100; % y-axis interval (used only for d>1))
h = 50; % physical stack height
z = 0; % height of observation (=0 for ground surface)
gplot = 1; % plot option (=1 yes; =0 no)
gcont = 2; % contour plot option (=2 filled; =1 yes; =0 none)
%----------------------------------execution-------------------------------
[x,y] = meshgrid (linspace(xmin,xmax,100),linspace(ymin,ymax,100));
c = zeros (size(x)); e = ones(size(x));
Dy = linspace(1,100,100); % in meters
Dz = Dy'; % in meters
[Dy,Dz] = meshgrid(Dz,Dy);
for i=1:100
for j=1:100
%...Pasquill-gifford for Dy
c = 24.167; d = 2.5334;%...Pasquill Stability Category is A
if x(i,j)<0.10; % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 18.3330; d = 1.8096;%...Pasquill Stability Category is B
if x(i,j)<0.20 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 12.5000; d = 1.0857;%...Pasquill Stability Category is C
if x(i,j) == 4; % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 8.3330; d = 0.72382;%...Pasquill Stability Category is D
if x(i,j)== 0.30 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 6.2500; d = 0.54287; %...Pasquill Stability Category is E
if x(i,j) < 0.10 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 4.1667; d = 0.36191; %...Pasquill Stability Category is F
if x(i,j) < 0.20 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
%...Pasquill-gifford for Dz
if x(i,j)<0.10 % x in kilometers
a = 122.8; b = 0.9447;%...stabilitas is A
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.100 <= x(i,j) && x(i,j) < 0.150
a = 158.08; b = 1.0542;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.150 <= x(i,j) && x(i,j) < 0.200
a= 170.22 ; b= 1.0932 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.200 <= x(i,j) && x(i,j) < 0.250
a= 179.52 ; b= 1.1262 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.250 <= x(i,j) && x(i,j) < 0.300
a= 217.41 ; b= 1.2644 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.300 <= x(i,j) && x(i,j) < 0.400
a= 258.89 ; b= 1.4094 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.400 <= x(i,j) && x(i,j) < 0.500
a= 346.75 ; b= 1.7283 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.500 <= x(i,j) && x(i,j) < 3.110
a= 453.85 ; b= 2.1166 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif x(i,j) >= 3.110
a= 453.85 ; b= 2.1166 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
if x(i,j)<0.20 % x in kilometers
a = 90.673; b = 0.93198;%...stabilitas is B
Dz(i,j)= a.*x(i,j).^b;
end
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.21 <= x(i,j) && x(i,j) < 0.40
a = 98.483; b = 0.98332;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
elseif x(i,j)>= 0.40
a = 109.400; b = 1.09710;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
if x(i,j) == all % x in kilometers
a = 61.141; b = 0.91465;%...stabilitas is C
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
if x(i,j)== 0.30 % x in kilometers
a = 34.459; b = 0.86974;%...stabilitas is D
Dz(i,j)= a.*x(i,j).^b;
end
if 0.31 <= x(i,j) && x(i,j) < 1.00
a = 32.093; b = 0.81066;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 3.00
a = 32.093; b = 0.64403;
Dz(i,j)= a.*x(i,j).^b;
end
if 3.01 <= x(i,j) && x(i,j) < 10.00
a = 33.504; b = 0.60486;
Dz(i,j)= a.*x(i,j).^b;
end
if 10.01 <= x(i,j) && x(i,j) < 30.00
a = 36.650; b = 0.56589;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j)>= 30.00
a = 44.053; b =0.51179;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) < 0.10 % x in kilometers
a = 24.260; b = 0.83660; %...stabilitas is E
Dz(i,j)= a.*x(i,j).^b;
end
if 0.10 <= x(i,j) && x(i,j) < 30.00
a = 23.331; b = 0.81956;
Dz(i,j)= a.*x(i,j).^b;
end
if 0.31 <= x(i,j) && x(i,j) < 1.00
a = 21.628; b = 0.75660;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 2.00
a = 21.628; b = 0.63077;
Dz(i,j)= a.*x(i,j).^b;
end
if 2.01 <= x(i,j) && x(i,j) < 4.00
a = 22.534; b = 0.57154;
Dz(i,j)= a.*x(i,j).^b;
end
if 4.01 <= x(i,j) && x(i,j) < 10.00
a = 24.703; b = 0.50527;
Dz(i,j)= a.*x(i,j).^b;
end
if 10.01 <= x(i,j) && x(i,j) < 20.00
a = 26.970; b = 0.46713;
Dz(i,j)= a.*x(i,j).^b;
end
if 20.01 <= x(i,j) && x(i,j) < 40.00
a = 35.420; b = 0.37615;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) >=40.00
a = 44.053; b = 0.51179;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) < 0.20 % x in kilometers
a = 15.209; b = 0.81558;%...stabilitas is F
Dz(i,j)= a.*x(i,j).^b;
end
if 0.21 <= x(i,j) && x(i,j) < 0.70
a = 14.457; b = 0.78407;
Dz(i,j)= a.*x(i,j).^b;
end
if 0.71 <= x(i,j) && x(i,j) < 1.00
a = 13.953; b = 0.68465;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 2.00
a = 13.953; b = 0.63227;
Dz(i,j)= a.*x(i,j).^b;
end
if 2.01 <= x(i,j) && x(i,j) < 3.00
a = 14.823; b = 0.54503;
Dz(i,j)= a.*x(i,j).^b;
end
if 3.01 <= x(i,j) && x(i,j) < 7.00
a = 16.187; b = 0.46490;
Dz(i,j)= a.*x(i,j).^b;
end
if 7.01 <= x(i,j) && x(i,j) < 15.00
a = 17.836; b = 0.41507;
Dz(i,j)= a.*x(i,j).^b;
end
if 15.01 <= x(i,j) && x(i,j) < 30.00
a = 22.651; b = 0.32681;
Dz(i,j)= a.*x(i,j).^b;
end
if 30.01 <= x(i,j) && x(i,j) < 60.00
a = 27.074; b = 0.27436;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j)>= 60.00
a = 34.219; b = 0.21716;
Dz(i,j)= a.*x(i,j).^b;
end
end
end
end
end
for i = 1:size(Q,2)
xx = x - xstack(i);
yy = y - ystack(i);
end
deltah(i,j) = V.*D./u.*1.5 + 2.68.*(10^(-3)).*P.*(Ts-Ta).*D./Ts;
H = h + deltah;
c1 = c + Q(i).*e./(4.*pi.*xx.*sqrt(Dy.*Dz)).*exp(-v.*yy.*yy./(4.*Dy.*xx)).*...
(exp(-v.*(z-H(i)).*(z-H(i)).*e./(4.*Dz.*xx))+exp(-v.*(z+H(i)).*(z+H(i)).*e./(4.*Dz.*xx)))...
.*exp(-lambda.*xx./v);
%--------------------------stack2---------------------------------------------
v = 0.5; % velocity
lambda = 0; % decay rate
Q = 1; % emission rate(s)
u = 3; % wind speed
P = 1; % Ambient Pressure
D = 6; % inside diameter
V = 22.4; % volumetric flow rate of stack gas
Ts = 400; % temperature of stack gas
Ta = 283; % temperature of ambient air
xstack = 0; ystack = 40; % stack location(s)
xmin = 0.5; xmax = 3.5; % x-axis interval
ymin = 0; ymax = 100; % y-axis interval (used only for d>1))
h = 50; % physical stack height
z = 0; % height of observation (=0 for ground surface)
gplot = 1; % plot option (=1 yes; =0 no)
gcont = 2; % contour plot option (=2 filled; =1 yes; =0 none)
%----------------------------------execution-------------------------------
[x,y] = meshgrid (linspace(xmin,xmax,100),linspace(ymin,ymax,100));
c = zeros (size(x)); e = ones(size(x));
Dy = linspace(1,100,100); % in meters
Dz = Dy'; % in meters
[Dy,Dz] = meshgrid(Dz,Dy);
for i=1:100
for j=1:100
%...Pasquill-gifford for Dy
c = 24.167; d = 2.5334;%...Pasquill Stability Category is A
if x(i,j)<0.10; % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 18.3330; d = 1.8096;%...Pasquill Stability Category is B
if x(i,j)<0.20 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 12.5000; d = 1.0857;%...Pasquill Stability Category is C
if x(i,j) == 4; % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 8.3330; d = 0.72382;%...Pasquill Stability Category is D
if x(i,j)== 0.30 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 6.2500; d = 0.54287; %...Pasquill Stability Category is E
if x(i,j) < 0.10 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
c = 4.1667; d = 0.36191; %...Pasquill Stability Category is F
if x(i,j) < 0.20 % x in kilometers
th = 0.017453293.*(c - d.*log(x(i,j)));
Dy(i,j) = 465.11628.*x(i,j).*(tan(th));
end
%...Pasquill-gifford for Dz
if x(i,j)<0.10 % x in kilometers
a = 122.8; b = 0.9447;%...stabilitas is A
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.100 <= x(i,j) && x(i,j) < 0.150
a = 158.08; b = 1.0542;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.150 <= x(i,j) && x(i,j) < 0.200
a= 170.22 ; b= 1.0932 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.200 <= x(i,j) && x(i,j) < 0.250
a= 179.52 ; b= 1.1262 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.250 <= x(i,j) && x(i,j) < 0.300
a= 217.41 ; b= 1.2644 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.300 <= x(i,j) && x(i,j) < 0.400
a= 258.89 ; b= 1.4094 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.400 <= x(i,j) && x(i,j) < 0.500
a= 346.75 ; b= 1.7283 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.500 <= x(i,j) && x(i,j) < 3.110
a= 453.85 ; b= 2.1166 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif x(i,j) >= 3.110
a= 453.85 ; b= 2.1166 ;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz(i,j)=5000;
end
if x(i,j)<0.20 % x in kilometers
a = 90.673; b = 0.93198;%...stabilitas is B
Dz(i,j)= a.*x(i,j).^b;
end
if Dz(i,j)>5000
Dz(i,j)=5000;
end
elseif 0.21 <= x(i,j) && x(i,j) < 0.40
a = 98.483; b = 0.98332;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
elseif x(i,j)>= 0.40
a = 109.400; b = 1.09710;
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
if x(i,j) == all % x in kilometers
a = 61.141; b = 0.91465;%...stabilitas is C
Dz(i,j)= a.*x(i,j).^b;
if Dz(i,j)>5000
Dz = 5000;
end
if x(i,j)== 0.30 % x in kilometers
a = 34.459; b = 0.86974;%...stabilitas is D
Dz(i,j)= a.*x(i,j).^b;
end
if 0.31 <= x(i,j) && x(i,j) < 1.00
a = 32.093; b = 0.81066;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 3.00
a = 32.093; b = 0.64403;
Dz(i,j)= a.*x(i,j).^b;
end
if 3.01 <= x(i,j) && x(i,j) < 10.00
a = 33.504; b = 0.60486;
Dz(i,j)= a.*x(i,j).^b;
end
if 10.01 <= x(i,j) && x(i,j) < 30.00
a = 36.650; b = 0.56589;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j)>= 30.00
a = 44.053; b =0.51179;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) < 0.10 % x in kilometers
a = 24.260; b = 0.83660; %...stabilitas is E
Dz(i,j)= a.*x(i,j).^b;
end
if 0.10 <= x(i,j) && x(i,j) < 30.00
a = 23.331; b = 0.81956;
Dz(i,j)= a.*x(i,j).^b;
end
if 0.31 <= x(i,j) && x(i,j) < 1.00
a = 21.628; b = 0.75660;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 2.00
a = 21.628; b = 0.63077;
Dz(i,j)= a.*x(i,j).^b;
end
if 2.01 <= x(i,j) && x(i,j) < 4.00
a = 22.534; b = 0.57154;
Dz(i,j)= a.*x(i,j).^b;
end
if 4.01 <= x(i,j) && x(i,j) < 10.00
a = 24.703; b = 0.50527;
Dz(i,j)= a.*x(i,j).^b;
end
if 10.01 <= x(i,j) && x(i,j) < 20.00
a = 26.970; b = 0.46713;
Dz(i,j)= a.*x(i,j).^b;
end
if 20.01 <= x(i,j) && x(i,j) < 40.00
a = 35.420; b = 0.37615;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) >=40.00
a = 44.053; b = 0.51179;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j) < 0.20 % x in kilometers
a = 15.209; b = 0.81558;%...stabilitas is F
Dz(i,j)= a.*x(i,j).^b;
end
if 0.21 <= x(i,j) && x(i,j) < 0.70
a = 14.457; b = 0.78407;
Dz(i,j)= a.*x(i,j).^b;
end
if 0.71 <= x(i,j) && x(i,j) < 1.00
a = 13.953; b = 0.68465;
Dz(i,j)= a.*x(i,j).^b;
end
if 1.01 <= x(i,j) && x(i,j) < 2.00
a = 13.953; b = 0.63227;
Dz(i,j)= a.*x(i,j).^b;
end
if 2.01 <= x(i,j) && x(i,j) < 3.00
a = 14.823; b = 0.54503;
Dz(i,j)= a.*x(i,j).^b;
end
if 3.01 <= x(i,j) && x(i,j) < 7.00
a = 16.187; b = 0.46490;
Dz(i,j)= a.*x(i,j).^b;
end
if 7.01 <= x(i,j) && x(i,j) < 15.00
a = 17.836; b = 0.41507;
Dz(i,j)= a.*x(i,j).^b;
end
if 15.01 <= x(i,j) && x(i,j) < 30.00
a = 22.651; b = 0.32681;
Dz(i,j)= a.*x(i,j).^b;
end
if 30.01 <= x(i,j) && x(i,j) < 60.00
a = 27.074; b = 0.27436;
Dz(i,j)= a.*x(i,j).^b;
end
if x(i,j)>= 60.00
a = 34.219; b = 0.21716;
Dz(i,j)= a.*x(i,j).^b;
end
end
end
end
end
for i = 1:size(Q,2)
xx = x - xstack(i);
yy = y - ystack(i);
end
deltah(i,j) = V.*D./u.*1.5 + 2.68.*(10^(-3)).*P.*(Ts-Ta).*D./Ts;
H = h + deltah;
c2 = c + Q(i).*e./(4.*pi.*xx.*sqrt(Dy.*Dz)).*exp(-v.*yy.*yy./(4.*Dy.*xx)).*...
(exp(-v.*(z-H(i)).*(z-H(i)).*e./(4.*Dz.*xx))+exp(-v.*(z+H(i)).*(z+H(i)).*e./(4.*Dz.*xx)))...
.*exp(-lambda.*xx./v);
%----------------------------------output----------------------------------
if gplot
for i = 10:10:100
plot (c1(:,i)); hold on;
plot (c2(:,i)); hold on;
end
end
if gcont
figure;
if gcont > 1
contourf (x,y,c); colorbar;
else
contour (x,y,c);
end
end
Anyone can help me please?!
When I run the code you have posted, I find that the 'c' variable is a single value, while 'x' and 'y' are matrices. So your problem is in the dimensions of the variables you try to plot together.
To debug your code try inserting a breakpoint in the code where the error occurs. Then when you run the code, it will stop at the breakpoint and you can check the values and dimensions of your variables.

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.