Safer alternative to MATLAB's `system` command
Thanks goes to Andrew Janke for helping me find this solution.
To easily reproduce the error we can run the command:
[ret, out] = system('sleep 2');
If we type some characters while this is running, the out
variable will be contaminated with what we typed.
The solution to this problem is to redirect stdin from /dev/null like the following:
[ret, out] = system('sleep 2 < /dev/null');
This stops the out
variable from being contaminated by user input.
Interestingly though this seems to fix the original test-case for the current MATLAB session (tested on R2014a OSX and R2013b Linux) so if we do another [ret, out] = system('sleep 2');
the output is no longer contaminated by user input.
Can matlab's int8 function be replaced with faster alternative
Since you are just using pixel_value
as an index into histogram
, you can use floor
instead of int8
to truncate the decimal component in the same manner.
In your example you indicate positive values, but if you expect negative vales, then you can use fix
.
But this doesn't seem faster... hmm.
SVD command in Python v/s MATLAB
To recover the Complete matrix you can do as follow :
import numpy as np
m = 10
n = 5
A=np.random.randn(m,n)
U,S,V =np.linalg.svd(A)
It's right that S.shape = (5,)
.
You want something similar to https://www.mathworks.com/help/matlab/ref/svd.html with A = 4x2
where final S = 4×2
too.
To do that you define a matrix B = np.zeros(A.shape)
. And you fill its diagonal with the element of S. By diagonal I mean where i==j
as follow :
B = np.zeros(A.shape)
for i in range(m) :
for j in range(n) :
if i == j : B[i,j] = S[j]
Now B.shape = (10,5)
as expected
Or in a more compact form :
C = np.array([[S[j] if i==j else 0 for j in range(n)] for i in range(m)])
I hope it helps
For the second question, I use gedit (standard text editor) running the code in ipython shell.
You can have a look to jupyter too
Matlab help, doc commands very slow
The problem has been solved with input from mathworks support:
>> tic;doc fwrite;toc
Elapsed time is 20.301202 seconds.
>> tic;reader = org.apache.lucene.index.IndexReader.open(fullfile(docroot,'helpsearch'));
searcher = org.apache.lucene.search.IndexSearcher(reader);
term = org.apache.lucene.index.Term('relpath','ref/plot.html');
query = org.apache.lucene.search.TermQuery(term);
hits = searcher.search(query);
fprintf('Found %d results\n', hits.length); searcher.close; reader.close; toc;
Java exception occurred:
java.io.IOException: Lock obtain timed out:
Lock@C:\Users\b\AppData\Local\Temp\lucene-ca3070c312bc20732565936b371a8bd3- commit.lock
at
org.apache.lucene.store.Lock.obtain(Lock.java:56)
at
org.apache.lucene.store.Lock$With.run(Lock.java:98)
at
org.apache.lucene.index.IndexReader.open(IndexReader.java:141)
at
org.apache.lucene.index.IndexReader.open(IndexReader.java:125)
After that, thinking it was a problem with a temp file being locked, I closed Matlab, went into AppData\Local\Temp\ and cleaned all the temporary files in it.
>> tic;
reader = org.apache.lucene.index.IndexReader.open(fullfile(docroot,'helpsearch'));
searcher = org.apache.lucene.search.IndexSearcher(reader);
term = org.apache.lucene.index.Term('relpath','ref/plot.html');
query = org.apache.lucene.search.TermQuery(term);
hits = searcher.search(query);
fprintf('Found %d results\n', hits.length); searcher.close; reader.close; toc;
Found 5 results
Elapsed time is 0.106868 seconds.
>> tic;doc fwrite;toc
Elapsed time is 0.153808 seconds.
Either it's the cleaning the temp files or the reference to internal java classes org.apache.lucene* that did the trick, but here it is, now doc is fast again.
MATLAB 'continue' command alternative
If you want to run the exact same code outside a loop, hence without being able to use continue
, you can simply rewrite it as follows:
if ~condition2
if condition1
statement1
else
statement2
end
statement3
end
Alternatively (I know it's not very elegant, but it does work indeed):
if condition1
statement1
statement3
elseif condition2
else
statement2
statement3
end
The above code be improved (a lot) by rewriting it as follows:
if condition1
statement1
statement3
elseif ~condition2
statement2
statement3
end
Finally, if your statement3
is particularly long and you don't want to repeat it twice, you can further improve the code above using a bypass flag:
go3 = false;
if condition1
statement1
go3 = true;
elseif ~condition2
statement2
go3 = true;
end
if go3
statement3
end
The problem is that abstract conditions don't allow me to use my imagination at full potential. Maybe if you specify the conditions you are using, even in a simplified way, I could try to come up with a better solution.
Matlab: Running an m-file from command-line
A command like this runs the m-file successfully:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\<a long path here>\mfile.m'); exit;"
How to exit a matlab m-file (NOT the matlab itself) if the user enters bad inputs?
You can just put a error command like error('bad user input') and it should stop the script.
Edit: alternatively, you could just refactor your code to not run unless you set the input flag to be true. Something like
inp = input('>', s)
if validateInput(inp)
%do you stuff here or call your main function
else
fprintf('Invalid input')
end
Related Topics
Openldap + Dynlist + Posixgroup
Git Post-Receive Checkout to Remote Machine
Use Grep to Remove Words from Dictionary Whose Roots Are Already Present
Using Flycheck/Flymake on Kernel Source Tree
Install Gdal on Python 2.7 on a Amazon Linux Virtual Server
How to Start a Nodejs Process on a Remote Server
Updating Shiny Server Config to Change Timeout Error
When Does a Process Handle a Signal
Ubuntu/Fedora: How to Add Applications in The Menus
Problems Building Libcurl 7.21.2 on Ubuntu 11.10 (Hiphop)
Linux/Unix Socket Self-Connection
Difference Between Different Ways of Running Shell Script
Cron Expression to Run on Different Days for Different Months