Get subexpression strings from output of pretty() in MATLAB - matlab
Is there a good way to get all the subexpressions in the output of a pretty() call in single-line strings? subexpr() returns a single subexpression, but I'd like to get all of them. Here's what pretty() returns:
syms x
s = solve(x^4 + 2*x + 1, x,'MaxDegree',3);
pretty(s)
/ -1 \
| |
| 2 1 |
| #2 - ---- + - |
| 9 #2 3 |
| |
| 1 #2 1 |
| ---- - #1 - -- + - |
| 9 #2 2 3 |
| |
| 1 #2 1 |
| #1 + ---- - -- + - |
\ 9 #2 2 3 /
where
/ 2 \
sqrt(3) | ---- + #2 | 1i
\ 9 #2 /
#1 == ------------------------
2
/ sqrt(11) sqrt(27) 17 \1/3
#2 == | ----------------- - -- |
\ 27 27 /
Here's what I'd like:
#1 == sqrt(3) ((2/(9 #2)) + #2) 1i) / 2
#2 == (sqrt(11) sqrt(27) / 27 - 17 / 27) ^ (1/3)
That way the output is easy cut-and-pastable into an editor for rapid conversion to code.
MATLAB functions ccode (or matlabFunction) do the trick beautifully.
syms x
s = solve(x^4 + 2*x + 1, x,'MaxDegree',3);
ccode(s, 'file', 'outfile.c');
Matlab generates outfile.c with sparse matrix notation and substitution-simplified computation:
t2 = sqrt(1.1E1);
t3 = sqrt(2.7E1);
t4 = t2*t3*(1.0/2.7E1);
t5 = t4-1.7E1/2.7E1;
t6 = 1.0/pow(t5,1.0/3.0);
t7 = pow(t5,1.0/3.0);
t8 = sqrt(3.0);
t9 = t6*(2.0/9.0);
t10 = t7+t9;
t11 = t6*(1.0/9.0);
A0[0][0] = -1.0;
A0[1][0] = t6*(-2.0/9.0)+t7+1.0/3.0;
A0[2][0] = t7*(-1.0/2.0)+t11-t8*t10*5.0E-1*sqrt(-1.0)+1.0/3.0;
A0[3][0] = t7*(-1.0/2.0)+t11+t8*t10*5.0E-1*sqrt(-1.0)+1.0/3.0;
Related
Understanding the | operator in scala [duplicate]
This question already has answers here: What are bitwise operators? (9 answers) Closed 3 years ago. I came to code: scala> val a = 0 | 1 a: Int = 1 scala> val a = 0 | 1 | 2 a: Int = 3 scala> val a = 0 | 1 | 2 | 3 a: Int = 3 scala> val a = 0 | 1 | 2 | 3 | 4 a: Int = 7 The only result I expected from the | operator is the result of the first command. I see it behaves like a logic or and also it adds elements in the second command. Could someone explain the working of the | operator using integers as operators?
| is the bitwise OR operator: val a = 0 | 1 a: Int = 1 00 0 01 1 ----- 01 1 val a = 0 | 1 | 2 | 3 a: Int = 3 00 0 01 1 10 2 11 3 ------ 11 3 val a = 0 | 1 | 2 | 3 | 4 a: Int = 7 000 0 001 1 010 2 011 3 100 4 ------- 111 7
It's just a logical or between each bit of binary representation of integer value (1 or 1 = 1, 1 or 0 = 1, 0 or 1 = 1, 0 or 0 = 0) val a = 0 | 1 //0 or 1 = 1 (1 - decimal number) val a = 0 | 1 | 2 //00 or 01 or 10 = 11 (3 - decimal number) val a = 0 | 1 | 2 | 3 //00 or 01 or 10 or 11 = 11 (3 - decimal number) val a = 0 | 1 | 2 | 3 | 4 //000 or 001 or 010 or 011 or 100 = 111 (7 - decimal number)
Stata merge with multiple match variables
I am having difficulty combining datasets for a project. Our primary dataset is organized by individual judges. It is an attribute dataset. judge j | x | y | z ----|----|----|---- 1 | 2 | 3 | 4 2 | 5 | 6 | 7 The second dataset is a case database. Each observation is a case and judges can appear in one of three variables. case case | j1 | j2 | j3 | year -----|----|----|----|----- 1 | 1 | 2 | 3 | 2002 2 | 2 | 3 | 1 | 1997 We would like to merge the case database into the attribute database, matching by judge. So, for each case that a judge appears in j1, j2, or j3, an observation for that case would be added creating a dataset that looks like below. combined j | x | y | z | case | year ---|----|----|----|-------|-------- 1 | 2 | 3 | 4 | 1 | 2002 1 | 2 | 3 | 4 | 2 | 1997 2 | 5 | 6 | 7 | 1 | 2002 2 | 5 | 6 | 7 | 2 | 1997 My best guess is to use rename j1 j merge 1:m j using case rename j j1 rename j2 j merge 1:m j using case However, I am unsure that this will work, especially since the merging dataset has three possible variables that the j identification can occur in.
Your examples are clear, but even better would be present them as code that would not require engineering edits to remove the scaffolding. See dataex from SSC (ssc inst dataex). It's a case of the missing reshape, I think. clear input j x y z 1 2 3 4 2 5 6 7 end save judge clear input case j1 j2 j3 year 1 1 2 3 2002 2 2 3 1 1997 end reshape long j , i(case) j(which) merge m:1 j using judge list +-------------------------------------------------------+ | case which j year x y z _merge | |-------------------------------------------------------| 1. | 1 1 1 2002 2 3 4 matched (3) | 2. | 2 3 1 1997 2 3 4 matched (3) | 3. | 2 1 2 1997 5 6 7 matched (3) | 4. | 1 2 2 2002 5 6 7 matched (3) | 5. | 2 2 3 1997 . . . master only (1) | |-------------------------------------------------------| 6. | 1 3 3 2002 . . . master only (1) | +-------------------------------------------------------+ drop if _merge < 3 list +---------------------------------------------------+ | case which j year x y z _merge | |---------------------------------------------------| 1. | 1 1 1 2002 2 3 4 matched (3) | 2. | 2 3 1 1997 2 3 4 matched (3) | 3. | 2 1 2 1997 5 6 7 matched (3) | 4. | 1 2 2 2002 5 6 7 matched (3) | +---------------------------------------------------+
Value of column, based on function over another column in Matlab table
I'm interested in the value of result that is in the same row as the min value of each column (and I have many columns, so I would like to loop over them, or do rowfun but I do not know how to get 'result' then). Table A +----+------+------+----+------+------+--------+ | x1 | x2 | x3 | x4 | x5 | x6 | result | +----+------+------+----+------+------+--------+ | 1 | 4 | 10 | 3 | 12 | 2 | 8 | | 10 | 2 | 8 | 1 | 12 | 3 | 10 | | 5 | 10 | 5 | 4 | 2 | 10 | 12 | +----+------+------+----+------+------+--------+ Solution 8 10 12 10 12 8 I know that I can apply rowfun, but then I don't know how to get result. And then, I can do this, but cannot loop over all the columns: A(cell2mat(A.x1) == min(cell2mat(A.x1)), 7) and I have tried several ways of making this into a variable but I can't make it work, so that: A(cell2mat(variable) == min(cell2mat(variable)), 7) Thank you!
Assuming your data is homogeneous you can use table2array and the second output of min to index your results: % Set up table x1 = [1 10 5]; x2 = [4 2 10]; x3 = [10 8 5]; x4 = [3 1 4]; x5 = [12 12 2]; x6 = [2 3 10]; result = [8 10 12]; t = table(x1.', x2.', x3.', x4.', x5.', x6.', result.', ... 'VariableNames', {'x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'result'}); % Convert A = table2array(t); % When passed a matrix, min finds minimum of each column by default % Exclude the results column, assumed to be the last [~, minrow] = min(A(:, 1:end-1)); solution = t.result(minrow)' Which returns: solution = 8 10 12 10 12 8 From the documentation for min: M = min(A) returns the smallest elements of A. <snip> If A is a matrix, then min(A) is a row vector containing the minimum value of each column.
Some Boolean Algebra Simplication basic
I wanna ask some basic law of boolean algebra. What i learn is : 1. A+A'B=A+B 2. A+AB'=A+B' 3. A+AB=A 4. A+A'B'=A+B' but i meet some condition like : A'+AB so, what is the answer for A'+AB?
Let's say A' = D so when A is false, then D is true and vice versa. Then A' + AB = D + D'B and if you understand your first equation: D + D'B = D + B = A' + B Regarding your comment: I'll use this equality: AB + A'B = B and I will combine the first with the third and the second with the fifth term: x'y'z'+x'yz+xy'z'+xy'z+xyz = y'z' + yz + xy'z Now, from the result, I can do this: y'z' + yz + xy'z = yz + y'(z' + zx) and now, using using A' + AB = A' + B: yz + y'(z' + zx) = yz + y'(z' + x) = yz + y'z' + y'x or do this: y'z' + yz + xy'z = y'z' + z(y+ xy') = y'z' + z(y + x) = y'z' + zy + xz Are they different? No, take a look at this: x y z | yz + y'z' + y'x | y'z' + zy + xz 0 0 0 | 1 | 1 0 0 1 | 0 | 0 0 1 0 | 0 | 0 0 1 1 | 1 | 1 1 0 0 | 1 | 1 1 0 1 | 1 | 1 1 1 0 | 0 | 0 1 1 1 | 1 | 1
You can use this open source project to solve basic boolean expression, its solve all the basic boolean expression
Difference between correctly / incorrectly classified instances in decision tree and confusion matrix in Weka
I have been using Weka’s J48 decision tree to classify frequencies of keywords in RSS feeds into target categories. And I think I may have a problem reconciling the generated decision tree with the number of correctly classified instances reported and in the confusion matrix. For example, one of my .arff files contains the following data extracts: #attribute Keyword_1_nasa_Frequency numeric #attribute Keyword_2_fish_Frequency numeric #attribute Keyword_3_kill_Frequency numeric #attribute Keyword_4_show_Frequency numeric ... #attribute Keyword_64_fear_Frequency numeric #attribute RSSFeedCategoryDescription {BFE,FCL,F,M, NCA, SNT,S} #data 0,0,0,34,0,0,0,0,0,40,0,0,0,0,0,0,0,0,0,0,24,0,0,0,0,13,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,BFE 0,0,0,10,0,0,0,0,0,11,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,BFE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,BFE ... 20,0,64,19,0,162,0,0,36,72,179,24,24,47,24,40,0,48,0,0,0,97,24,0,48,205,143,62,78, 0,0,216,0,36,24,24,0,0,24,0,0,0,0,140,24,0,0,0,0,72,176,0,0,144,48,0,38,0,284, 221,72,0,72,0,SNT ... 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,S And so on: there’s a total of 64 keywords (columns) and 570 rows where each one contains the frequency of a keyword in a feed for a day. In this case, there are 57 feeds for 10 days giving a total of 570 records to be classified. Each keyword is prefixed with a surrogate number and postfixed with ‘Frequency’. My use of the decision tree is with default parameters using 10x validation. Weka reports the following: Correctly Classified Instances 210 36.8421 % Incorrectly Classified Instances 360 63.1579 % With the following confusion matrix: === Confusion Matrix === a b c d e f g <-- classified as 11 0 0 0 39 0 0 | a = BFE 0 0 0 0 60 0 0 | b = FCL 1 0 5 0 72 0 2 | c = F 0 0 1 0 69 0 0 | d = M 3 0 0 0 153 0 4 | e = NCA 0 0 0 0 90 10 0 | f = SNT 0 0 0 0 19 0 31 | g = S The tree is as follows: Keyword_22_health_Frequency <= 0 | Keyword_7_open_Frequency <= 0 | | Keyword_52_libya_Frequency <= 0 | | | Keyword_21_job_Frequency <= 0 | | | | Keyword_48_pic_Frequency <= 0 | | | | | Keyword_63_world_Frequency <= 0 | | | | | | Keyword_26_day_Frequency <= 0: NCA (461.0/343.0) | | | | | | Keyword_26_day_Frequency > 0: BFE (8.0/3.0) | | | | | Keyword_63_world_Frequency > 0 | | | | | | Keyword_31_gaddafi_Frequency <= 0: S (4.0/1.0) | | | | | | Keyword_31_gaddafi_Frequency > 0: NCA (3.0) | | | | Keyword_48_pic_Frequency > 0: F (7.0) | | | Keyword_21_job_Frequency > 0: BFE (10.0/1.0) | | Keyword_52_libya_Frequency > 0: NCA (31.0) | Keyword_7_open_Frequency > 0 | | Keyword_31_gaddafi_Frequency <= 0: S (32.0/1.0) | | Keyword_31_gaddafi_Frequency > 0: NCA (4.0) Keyword_22_health_Frequency > 0: SNT (10.0) My question concerns reconciling the matrix to the tree or vice versa. As far as I understand the results, a rating like (461.0/343.0) indicates that 461 instances have been classified as NCA. But how can that be when the matrix reveals only 153? I am not sure how to interpret this so any help is welcome. Thanks in advance.
The number in parentheses at each leaf should be read as (number of total instances of this classification at this leaf / number of incorrect classifications at this leaf). In your example for the first NCA leaf, it says there are 461 test instances that were classified as NCA, and of those 461, there were 343 incorrect classifications. So there are 461-343 = 118 correctly classified instances at that leaf. Looking through your decision tree, note that NCA is also at other leaves. I count 118 + 3 + 31 + 4 = 156 correctly classified instances out of 461 + 3 + 31 + 4 = 499 total classifications of NCA. Your confusion matrix shows 153 correct classifications of NCA out of 39 + 60 + 72 + 69 + 153 + 90 + 19 = 502 total classifications of NCA. So there is a slight difference between the tree (156/499) and your confusion matrix (153/502). Note that if you are running Weka from the command-line, it outputs a tree and a confusion matrix for testing on all the training data and also for testing with cross-validation. Be careful that you are looking at the right matrix for the right tree. Weka outputs both training and test results, resulting in two pairs of matrix and tree. You may have mixed them up.