Editing Tips and tricks
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
=C++= | |||
== Real matrix operations== | |||
This is a table of matrix operations commonly performed in Octave and their equivalents in C++ when using the octave libraries. | This is a table of matrix operations commonly performed in Octave and their equivalents in C++ when using the octave libraries. | ||
Line 78: | Line 10: | ||
<tr><td>element multiplication</td><td><code>A.*B</code></td><td><code>product(A,B) </code></td></tr> | <tr><td>element multiplication</td><td><code>A.*B</code></td><td><code>product(A,B) </code></td></tr> | ||
<tr><td>element division</td><td><code>A./B</code></td><td><code>quotient(A,B) </code></td></tr> | <tr><td>element division</td><td><code>A./B</code></td><td><code>quotient(A,B) </code></td></tr> | ||
<tr><td>transpose*</td><td><code>A | <tr><td>transpose*</td><td><code>A'</code></td><td><code>A.transpose()</code></td></tr> | ||
<tr><td>select element m,n of A**</td><td><code>A(m,n)</code></td><td><code>A(m-1,n-1)</code></td></tr> | <tr><td>select element m,n of A**</td><td><code>A(m,n)</code></td><td><code>A(m-1,n-1)</code></td></tr> | ||
<tr><td>select row N of A**</td><td><code>A(N,:)</code></td><td><code>A.row(N-1)</code></td></tr> | <tr><td>select row N of A**</td><td><code>A(N,:)</code></td><td><code>A.row(N-1)</code></td></tr> | ||
Line 98: | Line 30: | ||
<tr><td>column vector</td><td><code>A(:)</code></td><td><code>ColumnVector(A.reshape (dim_vector(A.length())))</code></td></tr> | <tr><td>column vector</td><td><code>A(:)</code></td><td><code>ColumnVector(A.reshape (dim_vector(A.length())))</code></td></tr> | ||
<tr><td>row vector</td><td><code>A(:)'</code></td><td><code>RowVector(A.reshape (dim_vector(A.length())))</code></td></tr> | <tr><td>row vector</td><td><code>A(:)'</code></td><td><code>RowVector(A.reshape (dim_vector(A.length())))</code></td></tr> | ||
<tr><td>check for Inf or NaN</td><td><code>any(~isfinite(A))</code></td><td><code>A.any_element_is_inf_or_nan()</code></td></tr> | <tr><td>check for Inf or <a href="wiki.pl?NaN">NaN</a></td><td><code>any(~isfinite(A))</code></td><td><code>A.any_element_is_inf_or_nan()</code></td></tr> | ||
<tr><td>stack two matrices vertically</td><td><code>A=[B;C]</code></td><td><code>B.stack(C)</code></td></tr> | <tr><td>stack two matrices vertically</td><td><code>A=[B;C]</code></td><td><code>B.stack(C)</code></td></tr> | ||
<tr><td>uniform random matrix</td><td><code>rand(a,b)</code></td><td><code>octave_rand::distribution("uniform"); octave_rand::matrix(a,b)</code></td></tr> | <tr><td>uniform random matrix</td><td><code>rand(a,b)</code></td><td><code>octave_rand::distribution("uniform"); octave_rand::matrix(a,b)</code></td></tr> | ||
Line 113: | Line 45: | ||
<tr><td>number of rows</td><td><code>size(A,1)</code></td><td><code>A.rows()</code></td></tr> | <tr><td>number of rows</td><td><code>size(A,1)</code></td><td><code>A.rows()</code></td></tr> | ||
<tr><td>number of columns</td><td><code>size(A,2)</code></td><td><code>A.cols()</code></td></tr> | <tr><td>number of columns</td><td><code>size(A,2)</code></td><td><code>A.cols()</code></td></tr> | ||
</table> | </table> | ||
Notes: | Notes: | ||
<nowiki> | |||
*Transpose, addition, and multiplication operations also apply to RowVector, ComplexRowVector, ColumnVector, and ComplexColumnVector data types when the dimensions are in agreement. | |||
* | **The difference is due to the fact that arrays are zero-based in C++, but one-based in Octave. | ||
* | ***The names of Octave internal functions, such as mx_el_gt, are not documented and are subject to change. Functions such as mx_el_gt may eventually be available at both the scripting level and in C++ under more common names such as gt. | ||
</nowiki> | |||
* The names of Octave internal functions, such as mx_el_gt, are not documented and are subject to change. Functions such as mx_el_gt may eventually be available at both the scripting level and in C++ under more common names such as gt. | |||
==Complex Matrix Operations== | |||
<table> | <table> | ||
<tr><td><b>Operation</b></td><td><b>Octave</b></td><td><b>C++</b></td></tr> | <tr><td><b>Operation</b></td><td><b>Octave</b></td><td><b>C++</b></td></tr> | ||
Line 131: | Line 62: | ||
</table> | </table> | ||
=General= | |||
==How to declare functions inside a test block== | |||
==== | function experience | ||
%!test | |||
%! experience_design_mat | |||
%! experience_obs_eqs | |||
%! assert (experience_design_mat == pi); | |||
%! assert (experience_obs_eqs == exp(1)); | |||
%! | |||
%! endfunction % this is a trick. | |||
%! % now we can declare functions to be used by the test above. | |||
%! | |||
%! function a = experience_design_mat | |||
%! a = pi; | |||
%! endfunction | |||
%! | |||
%! function b = experience_obs_eqs | |||
%! b = exp(1); | |||
%! % endfunction: don't add it here. Let test() do it. | |||
==A funny formatting trick with fprintf found by chance== | |||
Imagine that you want to create a text table with fprintf with 2 columns of 15 characters width and both right justified. How to do this thing? | |||
That's easy: | |||
If the variable Text is a cell array of strings (of length <15) with two columns and a certain number of rows, simply type for the kth row of Text | |||
fprintf('%15.15s | %15.15s\n', Text{k,1}, Text{k,2}); | |||
The syntax '%<n>.<m>s' allocates '<n>' places to write chars and display the '<m>' first characters of the string to display. | |||
Example: | |||
octave:1> Text={'Hello','World'}; | |||
octave:2> fprintf('%15.15s | %15.15s\n', Text{1,1}, Text{1,2}) | |||
Hello | World | |||
==Load Comma Separated Values (*.csv) files== | |||
A=textread("file.csv", "%d", "delimiter", ","); | |||
B=textread("file.csv", "%s", "delimiter", ","); | |||
inds = isnan(A); | |||
B(!inds) = num2cell(A(!inds)) | |||
B = reshape(B, | |||
This gets you a 1 column cell array. You can reshape it to the original size by using the <code>reshape</function> | |||
The next version of octave (3.6) implements the <code>CollectOutput</code> switch as seen in example 8 here: http://www.mathworks.com/help/techdoc/ref/textscan.html |