Noncomformant argument in Octave - matlab

Why am I getting this error? Where is problem?
>> x=1:1:10
x =
1 2 3 4 5 6 7 8 9 10
>> y=(2*pi)/(x)
error: operator /: nonconformant arguments (op1 is 1x1, op2 is 1x10)

Related

conditional assignment in dolphindb

A matlab-like conditional assignment statement works for vectors in dolphindb:
x=1 2 3
x[x<2]=2
x;
[2,2,3]
But looks like a conditional assignment statement on tables doesn't work in dolphindb:
t=table(1 2 3 as x, 4 5 6 as y)
t[`x][t[`x]<2]=2
Syntax Error: [line #2] Please use '==' rather than '=' as equal operator in non-sql expression.
Anyone knows how to do it on tables?
Code:
x = [1 1 4 3]';
y = [4 1 5 6]';
t = table(x,y);
t.x(x<2) = 2
Results:
t =
x y
_ _
2 4
2 1
4 5
3 6

Except (^ or _dvl) analog in k4, 8 Queens example

I've just played with 8 Queens puzzle, and found out that it seems there is no _dvl operator (from k(v2)) in k(v4). Also I checked other k versions from ngn k impls and found ^ operator in k(v6), for example at JohnEarnest's impl:
l^a or l^l is except. Remove all instances of each of y from x.
k) 1 3 2 5 1 2 3^1 3 5
2 2
I really love SQL-style and would like to apply it in q. But is the following way idiomatic in q/k(v4) and is it a good solution? Or maybe there are shorter ways to do such list comparison/exclusion exists:
q)show s:til 8
0 1 2 3 4 5 6 7
q)s where not s in 2 4 6 /bother about this line, can it be shorter?
0 1 3 5 7
My version of q8 code is a bit longer then in nsl k2, without recursion and without conditions:
f:{raze {(x,) each (til 8) where not (til 8) in {x,(x-f),x+f:reverse 1+til count x} x} each x}
\ts:10 7 f/til 8 /248 100128
count 7 f/til 8 /92
first 7 f/til 8 /0 4 7 5 2 6 1 3
Upd: command I was looking for is except:
q)f:{raze {(x,) each (til 8) except {x,(x-f),x+f:reverse 1+til count x} x} each x}
Upd2: generalized 8 Queens solution in k(v4):
k){(x-1){,/{(x,)'(!y)#&~(!y)in{x,(x-f),x+f:|1+!#x}x}[;y]'x}[;x]/!x}8
Upd3: add 8 queens puzzle to blog
It is just keyword except.
How to find it: we know idiomatic k construction #&, so just search through .q namespace for it:
q)qfind:{([] q:k;k:.q k:key[.q] where (string value .q) like "*",x,"*")}
q)qfind "#&"
q k
-----------------------------------
inter k){x#&x in y}
except k){x#&~x in y}
xcols k){(x,f#&~(f:cols y)in x)#y}
q) (til 8) except 2 4 6
0 1 3 5 7

How to apply a function into every element

If I have vector [1,2,3,4],I hope to use a function f into every element to get
[f(1),f(2),f(3),f(4)]
If I have a matrix mat
>> mat=magic(3)
mat =
8 1 6
3 5 7
4 9 2
I hope to get
f(8) f(1) f(6)
f(3) f(5) f(7)
f(4) f(9) f(2)
Is any simple method to do this in matlab?
Solution
Use MATLAB's arrayfun function as follows:
arrayfun(f,mat)
Example
mat = magic(3); %defines input
f = #(x) x.^2; %defines f (f is the square function)
arrayfun(f,mat); %applies f on mat
Results
mat =
8 1 6
3 5 7
4 9 2
arrayfun(f,mat)=
64 1 36
9 25 49
16 81 4

MATLAB `fitglme` causes error on intermediate results

MATLAB R2014b's library function fitglme is acting up. It seems to be producing invalid intermediate results, cf. following run:
>> formula = 'Y ~ A + (1|B)';
>> glme = fitglme(ds,formula,'Verbose',2);
Starting PL iterations.
============================================================================================
ITER FUN VALUE NORM GRAD NORM STEP CG TERM RHO TRUST RAD ACCEPT
============================================================================================
0 -1.798e+308 0.000e+00 1.250e+03 BNDRY +1.747e+305 1.250e+03 YES
Infinity norm of the final gradient = 0.000e+00
Two norm of the final step = 1.250e+03, TolX = 1.000e-12
Relative infinity norm of the final gradient = 0.000e+00, TolFun = 1.000e-06
EXIT: Local minimum found.
-----------------------------------------------------------------------------------
PL ITER LOGLIK ||ETA|| ||ERR: ETA|| ||W|| ||ERR: ETA->MU->ETA||
-----------------------------------------------------------------------------------
1 NaN 2.797e+00 NaN 4.000e+00 NaN
Error using
classreg.regr.lmeutils.StandardLinearLikeMixedModel/validatey (line
299)
NaN or Inf values are not allowed in y.
Error in classreg.regr.lmeutils.StandardLinearMixedModel/set.y (line
265)
newy = validatey(slme,newy);
Error in
classreg.regr.lmeutils.StandardGeneralizedLinearMixedModel/fitUsingPL
(line 1661)
slme.y = ypw;
Error in
classreg.regr.lmeutils.StandardGeneralizedLinearMixedModel/refit
(line 4315)
[sglme,cause] = fitUsingPL(sglme,numIter,kappa);
Error in classreg.regr.lmeutils.StandardGeneralizedLinearMixedModel
(line 4288)
sglme = refit(sglme);
Error in GeneralizedLinearMixedModel/fitStandardLMEModel (line 1317)
slme =
classreg.regr.lmeutils.StandardGeneralizedLinearMixedModel(X,model.y,Zs,Psi,model.FitMethod,dofit,dostats,args{:});
Error in GeneralizedLinearMixedModel/fitter (line 891)
model.slme = fitStandardLMEModel(model);
Error in classreg.regr.FitObject/doFit (line 220)
model = fitter(model);
Error in GeneralizedLinearMixedModel.fit (line 2411)
model = doFit(model);
Error in fitglme (line 389)
glme = GeneralizedLinearMixedModel.fit(T,formula,varargin{:});
where
ds =
Y A B
2.7971 1 1
2.3801 2 1
1.7125 1 2
0.13291 2 2
0.70898 1 3
1.3898 2 3
0.55758 1 4
0.43072 2 4
-1.3622 1 5
-1.4441 2 5
-0.0781 1 6
0.48738 2 6
-0.77377 1 7
-1.4891 2 7
-1.149 1 8
-0.70913 2 8
Plese help. I am running thousands of these fittings, and I cannot tell why e.g. the data set here does not work, while e.g. the following DOES:
ds =
Y A B
2.8272 1 1
2.4091 2 1
1.6445 1 2
0.11834 2 2
0.66552 1 3
1.3342 2 3
0.53821 1 4
0.35225 2 4
-1.3412 1 5
-1.4446 2 5
-0.092893 1 6
0.44625 2 6
-0.805 1 7
-1.5075 2 7
-1.1167 1 8
-0.7717 2 8

Need detail explaination about "In an assignment A(I) = B, the number of elements in B and I must be the same"

So far, i don't get any ideas to understand this error mean. If I have, I think I could solve this problem in my matlab programming.
If anyone could help me to explain what's going on and give some example?
If I is a logical matrix
I =
1 0 1 1 0
and A is an array of the same size,
A =
5 3 8 1 2
then
>> A(I)
ans =
5 8 1
i.e., I works as a mask for the elements in A. You can also use this for assigment:
>> A(I) = 4
A =
4 3 4 4 2
(scalar assignment), or
>> A(I) = [1 NaN 3]
A =
1 3 NaN 3 2
(array assignment). You get the error if you try to do something like
>> A(I) = [ 7 6 5 4 8 7 6 8 7 9 4 3 2]
i.e., A(I) refers to a different number of elements than [7 6 5 4 8 7 6 8 7 9 4 3 2] contains. This is only valid when assigning with a scalar or empty value
A(I) = [] %# valid
A(I) = 4 %# valid
A(I) = [1 2 3] %# valid, numel(A(I)) == numel([1 2 3])
A(I) = [1 2] %# NOT VALID, numel(A(I)) != numel([1 2])