Can Octave combine elements and indices in expressions? - octave

I'd like to create a matrix of elements with each value in column i the ith power of the value in column 1. Easy with a for loop, but is there a way to combine matrix elements and their indices in expressions?

Do you mean something like this?
M = M(:,1) .^ (1:size(M,2));
It is easy to generate an array of indices to manipulate and/or operate on.
Note: For older versions of MATLAB the above gives an error, you need to use bsxfun:
M = bsxfun(#power, M(:,1), 1:size(M,2));
Note 2: If your inputs are v=[3;5;7] and n=3 you can translate the above to
M = v .^ (1:n);

What about this:
F = #(x, n) bsxfun (#realpow, x(:), 1:n);
Example:
>> F ([3;5;7], 3)
ans =
3 9 27
5 25 125
7 49 343

Related

how to show only multiple of 25 in a sql record?

I have a plenty of record in MedicineStock field. i would like to only show the multiple of 25 in MedicineStock.. multiple of 25 [ 50 - 75 - 100.. etc ]
here's query i try
SELECT MedicineID,MedicineName,MedicineStock
FROM c a, MsMedicineType b
WHERE a.MedicineTypeID = b.MedicineTypeID
AND MedicineStock = MedicineStock+25
ORDER BY MedicineName
but it's not working. i'm still student i try to read the modules the teacher give.. but don't find any good refreence for it. and already looking for some refreence in google and couldn't find it. i'm totally stuck. i hope i'll get to know how to do it. in here. thank you in advance
Use the MOD function which represents the modulus operation in math.
MOD() returns the remainder of a number divided by another number, in your case, all multiples of 25 have 0 as remainder.
SELECT MedicineID,MedicineName,MedicineStock
FROM c a, MsMedicineType b
WHERE a.MedicineTypeID = b.MedicineTypeID
AND MOD(a.MedicineTypeID,25) = 0
ORDER BY MedicineName

How to read a CNF file?

i'm working on argumentation system made by Dung
how to read this file content?
it's a cnf file describe an abstract argumentation framework
enter image description here
The file defines Boolean expressions written in Conjuctive Normal Form. Basically it’s clauses joined by AND, with each variable in the clause ORed together.
The first line of the file format (.cnf) starts with
p cnf num_variables num_clauses
So In your example 7 is the number of variables, 12 is the number of clauses.
Then each line defines a clause. For example:
(A v B v C) ^ (A v D v E) ^ (B v C v D)
number of variables is 5 (ABCDE), number of clauses is 3 (separated by AND)
The variable indexing starts from 1, wirh each clause on a seperate line. Each line ends with a 0 to signify end of the clause. To show a negation, add a “-“ (negative sign) before the variable.
p cnf 5 3
1 2 3 0
1 4 5 0
2 3 4 0
I don’t know what the e is in the first line.

Efficient MySQL Query Method to avoid multiple OR statement

I am quite new about queries and I would like to know if there is an easier solution for the query I am working on.
For instance I want to get the data where x is 5,7,9,11,13,15 and 17.
I have a query like below;
SELECT * FROM abc WHERE x = 5 or x = 7 or x = 9 or x = 11 or x = 13 or x = 15 or x = 17;
Is it okay to use this query or are there any other simpler and efficient solution?
EDIT
Does it affect the perfomance when I use x=[5,7,8,11,13,15,17] vs x=[5,11,7,15,8,17,13]
X is the ID of another category for instance.
This is shorter but performs equally
SELECT * FROM abc WHERE x in (5,7,9,11,13,15,17)
But remember if one entry in the in clause is null then it returns FALSE.

mySQL, two-dimensional into one-dimensional

I've forgotten whatever I used to know about pivots, but this seems to me the reverse. Suppose I have a set of items A, B, C, D, … and a list of attributes W, X, Y, Z. I have in a spreadsheet something like
A B C D
W 1 P 3 Q
X 5 R 7 S
Y T 2 U 4
Z D 6 F 7
where the value of attribute X for item B is 'P'. In order to do some statistics on comparisons, I'd like to change it from table to list, i.e.,
W A 1
X A 5
Y A T
Z A D
W B P
X B R
Y C U
Z C F
W D Q
X D S
Y B 2
Z B 6
Etc.
I can easily write a nested loop macro in the spreadsheet to do it, but is there an easy way to import it into mySQL in the desired format? Queries to get the statistics needed are simple in SQL (and formulas not very hard in a spreadsheet) if the data is in the second format.
Since there apparently isn't a "spreadsheet" tag, I used "excel." :-)
There are a lot of questions that looked similar at first glance, but the five I looked at all wanted to discard one of the indices (A-D or W-Z), i.e. creating something like
W 1
W P
X 5
X R
EDITED
You can use PowerQuery to unpivot tables. See the answer by teylyn for the following question. I have Office 365 and didn't need to install the plugin first. The functionality was already available.
Convert matrix to 3-column table ('reverse pivot', 'unpivot', 'flatten', 'normalize')
Another way to unpivot data without using VBA is with PowerQuery, a free add-in for Excel 2010 and higher, available here: http://www.microsoft.com/en-us/download/details.aspx?id=39379
...
Click the column header of the first column to select it. Then, on the Transform ribbon, click the Unpivot Columns drop-down and select Unpivot other columns.
...
OLD ANSWER
If you import the spreadsheet as is, you can run a query to output the correct format.
For a fixed, small number of items, you can use UNION for each of the columns.
SELECT attr, 'A' AS 'item', A AS 'value'
FROM sheet
UNION
SELECT attr, 'B' AS 'item', B AS 'value'
FROM sheet
UNION
SELECT attr, 'C' AS 'item', C AS 'value'
FROM sheet
UNION
SELECT attr, 'D' AS 'item', D AS 'value'
FROM sheet;
Working example: http://sqlfiddle.com/#!9/c274e7/7

A homework about growth rate of function

Please order the function belows by growth rate
n ^ 1.5
n ^ 0.5 + log n
n log ^ 2 n
n log ( n ^ 2 )
n log log n
n ^ 2 + log n
n log n
n
ps:
Ordering by growth rate means, as n gets larger and larger, which function will eventually be higher in value than the others.
ps2. I have ordered most of the functions:
n , n log log n, n log n, n log^2 n, n log ( n ^ 2 ), n ^ 1.5
I just do not know how to order:
n ^ 2 + log n,
n ^ 0.5 + log n,
these 2 values
Can anyone help me?
Thank you
You can figure this out fairly easily by graphing the functions and seeing which ones get larger (find a graphing calculator, check out Maxima, or try graphing the functions on Wolfram Alpha). Or, or course, you just pick some large value of n and compare the various functions, but graphs can give a bit of a better picture.
The key to the answer you seek is that when you sum two functions, their combined "growth rate" is going to be exactly that of the one with the higher growth rate of the two. So, you now know the growth rates of these two functions, since you appear (from knowing the correct ordering of all the others) to know the proper ordering of the growth rates that are in play here.
Plugging in a large number is not the correct way to approach this!
Since you have the order of growth, then you can use the following rules http://faculty.ksu.edu.sa/Alsalih/CSC311_10_11_01/3.3_GrowthofFunctionsAndAsymptoticNotations.pdf
In all of those cases, you're dealing with pairs of functions that themselves have different growth rates.
With that in mind, only the larger one really matters, since it will be most dominant even with a sum. So in each of those function sums, which is the bigger one and how does it compare to the other ones on your larger list?
If you need to proof mathematically, you should try something like this.
If you have two functions, e.g.:
f1(n) = n log n
f2(n) = n
You can simply find the limit of f3(n) = f1(n)/f2(n) when n tends to infinity.
If the result is zero, then f2(n) has a greater growth rate than f1(n).
On the other hand, if the result is infinity then f1(n) has a greater growth rate than f2(n).
n0.5 (or n1/2) is the square root of n. So, it grows more slowly than n2.
let say n = 4 then we get
n ^ 2 + log n = 16.6020599913
n ^ 1.5 = 8
n = 4
n log ( n ^ 2 ) = 4.81
n ^ 0.5 + log n = 2.60205999133
n log n = 2.4
n log ^ 2 n = ?
n log log n = -0.8