CoffeeScript's Modulus Operator's Coercion - coffeescript

Why does CoffeeScript's % operator coerce its argument to a number?
coffee> '3' % 3
0
coffee> '3a' % 3
NaN

Because that's what the Javascript spec says; ยง11.5:
5. Let leftNum be ToNumber(leftValue).

Related

How to call the dynamic variable that created from looping in Matlab?

I have 4 array create by using looping method:
n=4;
for i=1:n
eval(['Bucket' num2str(i) '= []'])
end
So the array of the output is:
Bucket1=[]
Bucket2=[]
Bucket3=[]
Bucket4=[]
then if I have a looping function for example:
while a<n
Bucket1 = 'Hello world';
a=a+1;
end
So how i can replace the Bucket1 to a dynamic variable? For example when a is equal to 1 then the Hello World will direct store in Bucket1. That's meant The variable name which is Bucket1 is not hard coding.
Can anyone share ideas with me? because I new to Matlab.
Given that the use of dynamic variable is a bad practice (as stated in the above comments), among the possible "alternative" solution (these also, already suggested in the above comments) the use of the struct data type can be considered coupled with the use of the dynamic field names.
The advantage of the dynamic field names consists in having the possibility to "programmatically" define the names of the variables (which in this case will be the field of the struct) avoiding the potential danger implied by eval.
Also the use of a struct allows to manage its field and their content programmatically by using the very large set of related functions.
In the following "crazy" implementation, the dynamic field names concept is used to create a set of variables as fields of a struct.
In particular, the code allows to create:
a set of variables (struct's field) whose name are defined in a cellarray:
dynamic_vars={'Bucket','another_var','one_more_var'}
for each of the variables, is then possible to specify the number of (that is, for example, 4 Bucket => Bucket_1, Bucket_2, .... The quantity is specified in an array
how_many=[4 3 2]
then it is possible to specify, in a cellarray the type of the variables (double, char, cell)
var_type={'double' 'char' 'cell'}
for each of the above varaibles / struct's field is possible to specify a way to initialize them through functions such as zeros, nan, ones or a string
init_set={'NaN' 'Hellp World' 'zeros'}
to complete the definition and initialization of the variables, it is possible to set their size using a cellarray:
var_dim={[2 3] -1 [1 3] [2 3]}
This is the full code:
% Define the name of the variable
dynamic_vars={'Bucket','another_var','one_more_var'}
% Define how many variables to be created with the "dynamic_vars" names
how_many=[4 3 2]
% Define the type of the variable to be created
var_type={'double' 'char' 'cell'}
% Define the function or string to be used to initialize the variables
init_set={'NaN' 'Hellp World' 'zeros'}
%init_set={'NaN' 'Hellp World' 'char'}
% Define the size of the dynamic_vars tobe created
% In the case of cellarray:
% if cellarray of "number" two dimension have to be provided:
% size of the cellarray
% size of the content of the cellarray
% if cellarray of string to specify:
% the size of the cellarray
% the string to be used to initialize the cellarray
var_dim={[2 3] -1 [1 3] [2 3]}
%var_dim={[2 3] -1 [1 3] 'dummy_str'}
n_var=length(dynamic_vars)
% Loop over the variables to be created
for i=1:n_var
% Loop over the number of variables to be created
for j=1:how_many(i)
% Select the data type of the variable
switch(var_type{i})
% Create the i-th variable of the j-th type and iknitialize it
case 'double'
switch(init_set{i})
case 'zeros'
my_data.([dynamic_vars{i} '_' num2str(j)])=zeros([var_dim{i}])
case 'NaN'
my_data.([dynamic_vars{i} '_' num2str(j)])=nan([var_dim{i}])
case 'ones'
my_data.([dynamic_vars{i} '_' num2str(j)])=ones([var_dim{i}])
case 'rand'
my_data.([dynamic_vars{i} '_' num2str(j)])=rand([var_dim{i}])
otherwise
disp('ERROR: Unvalid init_set')
return
end
case 'char'
my_data.([dynamic_vars{i} '_' num2str(j)])=init_set{i}
case 'cell'
switch(init_set{i})
case 'char'
my_data.([dynamic_vars{i} '_' num2str(j)])=repmat({var_dim{i+1}},[var_dim{i}])
case 'zeros'
my_data.([dynamic_vars{i} '_' num2str(j)])=repmat({zeros(var_dim{i+1})},[var_dim{i}])
case 'NaN'
my_data.([dynamic_vars{i} '_' num2str(j)])=repmat({nan(var_dim{i+1})},[var_dim{i}])
case 'ones'
my_data.([dynamic_vars{i} '_' num2str(j)])=repmat({ones(var_dim{i+1})},[var_dim{i}])
case 'rand'
my_data.([dynamic_vars{i} '_' num2str(j)])=repmat({rand(var_dim{i+1})},[var_dim{i}])
otherwise
disp('ERROR: Unvalid init_set')
return
end
otherwise
disp('ERROR: Unvalid data type')
return
end
end
end
my_data
which generate the struct my_data with the following fields:
Bucket_1
Bucket_2
Bucket_3
Bucket_4
another_var_1
another_var_2
another_var_3
one_more_var_1
one_more_var_2
Initialized as follows:
Bucket_1 =
NaN NaN NaN
NaN NaN NaN
Bucket_2 =
NaN NaN NaN
NaN NaN NaN
Bucket_3 =
NaN NaN NaN
NaN NaN NaN
Bucket_4 =
NaN NaN NaN
NaN NaN NaN
another_var_1 = Hellp World
another_var_2 = Hellp World
another_var_3 = Hellp World
one_more_var_1 =
{
[1,1] =
0 0 0
0 0 0
[1,2] =
0 0 0
0 0 0
[1,3] =
0 0 0
0 0 0
}
Caveat
Controls on the consistency of the input (e. g. the length of dynamic_vars and how_many must be the same, ...) have to be added
The code has been tested with Octave 4.2.1
This can be done using assignin as follows,
a=1;
while a<=n
assignin('base',['Bucket', num2str(a)], 'Hello world');
a=a+1;
end

le function in matlab behaves in this weird fashion. why?

when entering the logical statement that says
le 4 5
MATLAB returns 1. However if one does,
x=4;
and then
le x 5
MATLAB returns 0. Why is this true and how can I successfully have MATLAB correctly evaluate whether expression is less than or equal to number?
MATLAB has 2 function calling syntaxes, command and function.
Command syntax, used here, treats the inputs as character vectors. To make the logical comparison to a double, MATLAB implicitly converts 'x' to a double, 120, which is its ASCII equivalent. le(120, 5) will obviously evaluate false.
Use function syntax:
>> x = 4; le(x, 5)
ans =
logical
1
You need to use the parenthesis as le is a function with arguments x and 5.
Typing: le(x,5) will return 1.

behavior of colon operator (:) with matrix or vector arguments

We all know the matlab colon operator to create a linear sequence, i.e.
1:5 = [1 2 3 4 5]
Now I found that the arguments of the colon operator can also be applied to vectors or matrices. However I do not understand the definition behind.
Examples
[1 2 3 4]:5 == [1 2 3 4 5]
[1 2; 3 4]:3 == [1 2 3]
Why is this?
The second argument can be vector or matrix as well.
Ultimately I would like to understand sequences such as
1:2:3:4:5
which is fully legal in matlab and [1 5] by the way!
Note 1:2:3:4:5:6 is left associative i.e. parsed as ((1:2:3):4:5):6.
So what is the behavior for the colon operator with matrix/vector arguments?
EDIT: corrected the statement of left associativity.
The documentation for the colon operator says:
If you specify nonscalar arrays, MATLAB interprets j:i:k as j(1):i(1):k(1).
Your first example is interpreted as 1:3, the second as 1:5
Expressions with more than two : are parsed left-associative:
a:b:c:d:e==(a:b:c):d:e
.
>> 1:2:3:4:5
ans =
1 5

~= operator in Matlab

Does anyone know what the operator ~= in Matlab? I have seen it in an if statement
if currsign ~= 0
[
]
Not equal to?
It simply means "not equal"
For example:
1 ~= 1 % Returns false
1 ~= 2 % Returns true
In Matlab ~= is not equal to. the brackets following the if are not valid Matlab syntax though. 'if' needs to be paired with an 'end'

How can I make XOR work for logical matrix in MATLAB?

>> XOR(X,X)
??? Undefined function or method 'XOR' for input arguments of type 'logical'.
Why XOR can't be used for logical matrix?
And I tried a more simple example:
>> A=[1 0;1 0];
>> B=[1 1;0 0];
>> XOR(A,B)
??? Undefined function or method 'XOR' for input arguments of type 'double'.
How can I properly use XOR?
It works for me.
A=[1 0;1 0];
B=[1 1;0 0];
xor(A,B)
ans =
0 1
1 0
Yet when I try this...
XOR(A,B)
??? Undefined function or method 'XOR' for input arguments of type 'double'.
See the difference. Leave caps off to fix the problem.
I think the ambiguity arises because of a MathWorks convention used in their documentation. When they show the name of a function in their help, they use all caps. For example, here is the help for xor.
>> help xor
XOR Logical EXCLUSIVE OR.
XOR(S,T) is the logical symmetric difference of elements S and T.
The result is logical 1 (TRUE) where either S or T, but not both, is
nonzero. The result is logical 0 (FALSE) where S and T are both zero
or nonzero. S and T must have the same dimensions (or one can be a
scalar).
Even so, when you use the function, you do so with lower case letters in the function name.
How about the following:
C = abs(A-B);
The statement above makes C the XOR of A and B, because xor is true where the entries are different from each other, and 1-0 or 0-1 will give 1 or -1 (and abs of that will give 1), while 0-0 and 1-1 are both 1.
If you really want, you can create an "XOR.m" file with the following definition:
function C=XOR(A,B)
% function C=XOR(A,B)
% INPUTS:
% A - m x n matrix, consisting only of 1s or 0s.
% B - m x n matrix, consisting only of 1s or 0s.
% OUTPUT:
% C - m x n matrix, containing the logical XOR of the elements of A and B
C=abs(A-B)
However, you should keep in mind that function calls in Matlab are horrifically slow, so you might want to just write out the definition that I gave you wherever you happen to need it.
Edit
I did not originally understand your question.... you need to use xor and not XOR, and if it is complaining that your matrices are doubles instead of logicals, then use A==1 and B==1 instead of A and B. Matlab is case sensitive when it comes to variable names and built-in functions such as the xor function.
See this post. C = A~=B