How can I make the following variable on SPSS? - loops

I have a variable with values from -100 to 100.
I want a new variable where -14 to 14 will be 1 and all others will be 0.
I have this so far but I get an error.
DO IF (Rad_Start_Minus_Chemo_start GT -14).
Compute NACRT =1.
ELSE IF (Rad_Start_Minus_Chemo_start LT 14).
COMPUTE NACRT=1
ELSE IF (Rad_Start_Minus_Chemo_start GT 14).
Compute NACRT=0.
ELSE IF (Rad_Start_Minus_Chemo_start LT -14).
Compute NACRT=0.
END IF.

You may use RECODE:
RECODE Rad_Start_Minus_Chemo_start (-14 THRU 14 = 1) (ELSE = 0) /INTO NACRT.
or
COMPUTE NACRT = RANGE(Rad_Start_Minus_Chemo_start, -14,14).
(this will compute NACRT as a boolean 0/1 variabile; less flexible than recode, but in your case it gets the job done).

Related

How to add additional zero arrrays

I have the following problem in my simulation.
A is an array 24 x 2. I am going to split it and get 4 or 12 array. It means that I group 6 or 2 array. It will be ok, if I use even "split" coefficient. If it is odd, I can"t split A.[ I can't group 5 or 7, because of 24/5=4*5 + 4 ( or 5*5 -1) or 24/7=7*3+3.
That's why I going to do the following:
If I have 24 x 2 and need group every 5 together:
block 1 : A(1,:), A(2,:),A(3,:),A(4,:),A(5,:)
block 2 : A(6,:), A(7,:),A(8,:),A(9,:),A(10,:)
block 3 : A(11,:), A(12,:),A(13,:),A(14,:),A(15,:)
block 4 : A(16,:), A(17,:),A(18,:),A(19,:),A(20,:)
block 5 : A(21,:), A(22,:),A(23,:),A(24,:), ?
As you can see the 5th block is not full, Matlab gives me an error. My idea is to create A(25,:)=0. For my simulation it will be ok.
I am going to simulate it as function:
A=rand(m,n)
w- # number of a vector that i would like group together ( in ex., it is `5`)
if mod(w,2)==0
if mod(m,2)==0
% do....
else
% remainder = 0
end
else
if mod(m,2)==0
% remainder = 0
else
%do...
end
I was going to simulate like above, but then I have noticed that it doesn't work. Because 24/10 = 2*10+4. So I should write something else
I can find the reminder as r = rem(24,5). As an example above, MatLab gives me r=4. then I can find a difference c= w-r =1 and after that, I don't know how to do that.
Could you suggest to me how to simulate such a calculation?
Determine the number of blocks needed, calculate the virtual amount of rows needed to fill these blocks, and add as many zero rows to A as the difference between the virtual and actual amount of rows. Since you didn't mention, what the actual output should look like (array, cell array, ...), I chose a reshaped array.
Here's the code:
m = 24;
n = 2;
w = 5;
A = rand(m, n)
% Determine number of blocks
n_blocks = ceil(m / w);
% Add zero rows to A
A(m+1:w*n_blocks, :) = 0
% Reshape A into desired format
A = reshape(A.', size(A, 1) / n_blocks * n, n_blocks).'
The output (shortened):
A =
0.9164959 0.1373036
0.5588065 0.1303052
0.4913387 0.6540321
0.5711623 0.1937039
0.7231415 0.8142444
0.9348675 0.8623844
[...]
0.8372621 0.4571067
0.5531564 0.9138423
A =
0.91650 0.13730
0.55881 0.13031
0.49134 0.65403
0.57116 0.19370
0.72314 0.81424
0.93487 0.86238
[...]
0.83726 0.45711
0.55316 0.91384
0.00000 0.00000
A =
0.91650 0.13730 0.55881 0.13031 0.49134 0.65403 0.57116 0.19370 0.72314 0.81424
0.93487 0.86238 0.61128 0.15006 0.43861 0.07667 0.94387 0.85875 0.43247 0.03105
0.48887 0.67998 0.42381 0.77707 0.93337 0.96875 0.88552 0.43617 0.06198 0.80826
0.08087 0.48928 0.46514 0.69252 0.84122 0.77548 0.90480 0.16924 0.82599 0.82780
0.49048 0.00514 0.99615 0.42366 0.83726 0.45711 0.55316 0.91384 0.00000 0.00000
Hope that helps!

matlab complex for-loop correlation calcul

This is the script that I have. It works till the ------ separation. Under I do not get any error from Matlab, but neither do I get a return of bestDx nor bestDy. Please help. (The first part is given just to put you in context)
%%
% Variables after running script Read_eA3_file.m
%date_time_UTC
%reflectivity
%clutter_mask
%Convert units
dBZ = reflectivity * 0.375 - 30;
dBZ_Mask = clutter_mask * 0.375 - 30;
%Replace clutter values with NaN
weather = NaN(size(dBZ)); %initialise to constant
weather(dBZ>=dBZ_Mask) = dBZ(dBZ>=dBZ_Mask); %copy values when A >= B
%Reduce to range -- those are 384x384 arrays
dBZ_range = dBZ(:,:,1:16); %16:18 to 16:23 included
weather_range = weather(:,:,1:16); %16:18 to 16:23 included
weather1618 = weather(:,:,1); %16:18 map only
weather1623 = weather(:,:,16); %16:23 map only
% Plot maps
image(imrotate(-weather1618,90)); %of 16:18
image(imrotate(-weather1623,90)); %of 16:23
%Find x,y of strongest dBZ
%Since the value are all negative. I look for their minimun
[M,I] = min(weather1618(:)); %for 16:18
[I_row, I_col] = ind2sub(size(weather1618),I); %values are 255 and 143
[M2,I2] = min(weather1623(:)); %for 16:23
[I2_row, I2_col] = ind2sub(size(weather1623),I2); %values are 223 and 7
%Calc displacement
%I get a value of 139.7140
max_displ=sqrt((I2_row-I_row)^2+(I2_col-I_col)^2); %between 1618 and 1623
%%
% -----Section below does not work; ONLY RUN the section ABOVE---------
%% Find Dx Dy for max_corr between two maps
maxCoeff=0;
weather1618Modified = zeros(384,384); %create weather array for time range
%weather1618Modified(:) = {NaN}; % Matlab cannot mix cell & double
%%
for x = 1:384
for y = 1:384
%30 pixel appx.
for Dx = -max_displ:30: max_displ
for Dy = -max_displ:30: max_displ
%Limit range of x+Dx and y+Dy to 1:384
if x+Dx<1 | y+Dy<1 | x+Dx>384 | y+Dy>384
continue
%weather1618Modified is the forecasted weather1823
weather1618Modified(x+Dx,y+Dy) = weather1618(x,y)
%Find the best correlation; Is corrcoef the right formula?
newCoeff=corrcoef(weather1623,weather1618Modified);
if newCoeff>maxCoeff
maxCoeff=newCoeff;
bestDx=Dx;
bestDy=Dy;
end
end
end
end
end
end
%% Calc displacement
bestDispl = sqrt(bestDx^2+bestDy^2); %bestDispl for a 5 min frame
%Calc speed
speed = bestDispl/time;
You have to delete the continue statement after the first if (or place it somewhere else).
The continue statement makes the program skip the remaining part of the for-loop and go directly to the next iteration. Therefore bestDx and bestDy will never be set.
Documentation: https://se.mathworks.com/help/matlab/ref/continue.html

MATLAB sort function yields tampered results

I have a vector of 126 elements which is usually correctly sorted; however, I always sort it to make sure everything is okay.
The problem is that: when the array is already sorted, performing a sort would destroy the original values of the array.
I attached the array in a csv file and executed the script below, where I insert the vector in the first column of 'a' then sort it in the second then check for any differences in the third column.
a = csvread('a.csv')
a(:,2)=sort(a(:,1))
a(:,3)=a(:,2)-a(:,1)
result=sum(a(:,3).^2)
You could easily see that the first two columns aren't identical, and the third column has some none zero values.
Syntax for array
a = [17.4800
18.6800
19.8800
21.0800
22.2800
23.4800
24.6800
25.8800
27.0800
28.2800
29.4800
30.6800
46.1600
47.3600
48.5600
49.7600
50.9600
52.1600
53.3600
54.5600
55.7600
56.9600
58.1600
59.3600
74.8400
76.0400
77.2400
78.4400
79.6400
80.8400
103.5200
104.7200
105.9200
107.1200
108.3200
109.5200
110.7200
111.9200
113.1200
114.3200
115.5200
116.7200
132.2000
133.4000
134.6000
135.8000
137.0000
138.2000
139.4000
140.6000
141.8000
143.0000
144.2000
145.4000
165.4200
166.6200
167.8200
169.0200
170.2200
171.4200
172.6200
173.8200
175.0200
176.2200
177.4200
178.6200
179.9300
181.1300
182.3300
183.5300
184.7300
185.9300
187.1300
188.3300
189.5300
201.3700
202.5700
203.7700
204.9700
206.1700
207.3700
236.1100
237.3100
238.5100
239.7100
240.9100
242.1100
243.3100
244.5100
245.7100
246.9100
248.1100
249.3100
239.8400
241.0400
242.2400
276.9900
278.1900
279.3900
280.5900
281.7900
282.9900
284.1900
285.3900
286.5900
287.7900
288.9900
290.1900
277.8200
279.0200
280.2200
281.4200
282.6200
283.8200
285.0200
286.2200
287.4200
288.6200
289.8200
291.0200
291.0700
292.2700
293.4700
295.6900
296.8900
298.0900];
Your original vector is unfortunately not sorted. Therefore, sorting this result will obviously not give you what the original vector is supposed to be as the values that were out of order will become in order.
You can check this by using diff on the read in vector from the CSV file and seeing if there are any negative differences. diff takes the difference between the (i+1)th value and the ith value and if your values are monotonically increasing, you should get positive differences all around. We can see which locations are affected by finding values in the difference that are negative:
a = csvread('a.csv');
ind = find(diff(a) < 0);
We get:
>> ind
ind =
93
108
This says that locations 93 and 108 are where the out of order starts. Locations 94 and 109 is where it actually happens. Let's check out portions 90 - 110 of your vector to be sure:
>> a(90:110)
ans =
245.7100 % 90
246.9100 % 91
248.1100 % 92
249.3100 % 93
239.8400 %<-------
241.0400
242.2400
276.9900
278.1900
279.3900
280.5900
281.7900
282.9900
284.1900
285.3900
286.5900
287.7900 % 106
288.9900 % 107
290.1900 % 108
277.8200 % <------
279.0200
As you can see, locations 93 and 108 take a dip in numerical value, and so if you tried sorting the result then taking the difference, you'll notice that locations 1 up to 93 will exhibit a difference of 0, but after location 93, that's when it becomes unequal.
I'm frankly surprised you didn't see that they're out of order because your snapshot clearly shows there's a decrease in value on the left column towards the top of the snapshot.
Therefore, either check your data to see if you have input it correctly, or modify whatever process you're working on to ensure that it can handled unsorted data.

Need to verify I am understanding SQL Logic

I would like to know if I have this SQL logic decoded correctly. Here is the the SQL:
,[hrs].[Hours] - SUM(CASE WHEN [UnitState].[UnitStateType] <> 'ACTIVE' THEN [Allocation].[AllocatedEnergyMwh] ELSE 0 END / CAST([Unit].[NetDependableCapacity] AS FLOAT)) AS SH
I interpret this as saying:
if [UnitState].[UnitStateType] does not equal active then SH equals the sum of [Allocation].[AllocatedEnergyMwh] /
(float)[Unit].[NetDependableCapacity].
else SH = [hrs].[Hours]
Not exactly.
There is no else for SH. The else only affects the sum aggregate. More accurately, it says:
SH = hours -
(the sum of AllocatedEnergyMwh where StateType != ACTIVE) / NetDependableCapacity
The else is only used to ignore the active AllocatedEnergyMwh in the sum. It does this by setting AllocatedEnergyMwh = 0 in those cases.
It's close but you missed the [hrs].[Hours] - case part for the first one.
if [UnitState].[UnitStateType] does not equal active
then SH equals [hrs].[Hours] minus the sum of [Allocation].[AllocatedEnergyMwh] / (float)[Unit].[NetDependableCapacity].
else SH = [hrs].[Hours] minus (0)/[Unit].[NetDependableCapacity]
if [UnitState].[UnitStateType] does not equal active then
[hrs].[Hours] minus sum([Allocation].[AllocatedEnergyMwh] / [Unit].[NetDependableCapacity])
otherwise
[hrs].[Hours] minus sum(0/[Unit].[NetDependableCapacity]) -->meaning [hrs].[Hours] - 0

Check overlapping series in C#

I am making an application where the user adds start and end to define a range
The condition is that the range should not overlap:
How to check whether a number range is not overlapping e.g.
Range 1 Start 5 End 15
Range 2 Start 1 End 4
Range 3 Start 16 End 20
Range 4 Start 2 End 4
So the Range 4 makes the set invalid, how do I check this in C#.
Further the user can add the range in any order as in the example above, the entire series should be non overlapping.
Thanks for the help suggestion.
Regards,
Sakshi
Answer:
I made the solution is it correct:
If start and end is the range which needs to be validated then
start >startRange and start less than endRange
end>startRange and end less than endRange
The above 2 condition validates that the series is overlapping.
Where startRange and endRange is the start and end for all existing ranges.
The OP suggests two checks to validate that a new range does not overlap with an existing range. However, these two checks do not cover all the possibilities. For instance, if the existing range is (4,10) and the new range is (2,12), it will not be flagged, because it starts prior to the start of the existing range, and ends afterwards.
Instead, I'd recommend the following approach:
if (newRangeStart <= existingRangeEnd && newRangeEnd >= existingRangeStart) {
// we have an overlap
}
Essentially, there are four possibilities of overlapping ranges:
A range which starts before the existing range and ends within the existing range
A range which starts within the existing range and ends afterward
A range which starts within the existing range and ends within the existing range
A range which starts before the existing range and ends after the existing range
Cases (1) and (2) include partial overlap, while cases (3) and (4) include complete overlap (either the existing range completely encloses the new range [case 3] or the new range completely encloses the existing range [case 4]).
The OP's code catches cases 1, 2 and 3, but not case 4. The code here catches all 4 possibilities of an overlapping range.
Ranges need to have multiple checks:
You can have many variations of overlap, so you'll need to do several tests.
|-----------| |--------------|
|------------------------|
|-------------|
|--------------------------------|
First Check:
Start of Range 1 >= Start of Range 2 and Start of Range 1 <= End of Range 2
Second Check:
Start of Range 2 >= Start of Range 1 and Start of Range 2 <= End of Range 1
Third Check:
End of Range 1 >= Start of Range 2 and End of Range 1 <= End of Range 2
Fourth Check:
End of Range 2 >= Start of Range 1 and End of Range 2 <= End of Range 1
These checks assume that End >= Start on both ranges. If not, you'll need to swap the start and end for the tests.
public static bool DoRangesOverlap(int p_start1, int p_end1, int p_start2, int p_end2)
{
if ((p_start1 >= p_start2 && p_start1 <= p_end2) || (p_start2 >= p_start1 && p_start2 <= p_end1) || (p_end1 >= p_start2 && p_end1 <= p_end2) || (p_end2 >= p_start1 && p_end2 <= p_end1))
{
return true;
}
return false;
}

Resources