I'm trying to get the max indexes by row from a matrix. To do this, I'm doing:
[throwaway, indexes] = max(blah, [], 2);
The variable "throwaway," would store values I don't want anymore and would never use, and I don't want to waste memory on it. Is there some way to indicate I don't want anything put into that throwaway variable? Something like undef in Perl, perhaps?
Yes, you can throw away return arguments with ~ for examples if func returns two arguments only v will be available after the following [~, v] = func()
Related
For example, I want to pass a dictionary with 10 pairs into a function to bypass the 8 valence limit. I then want the keys of the dictionary each to be assigned as a local variable to be assigned to their value and use that as the parameters for my function. Alternatively, is there a better way to pull this off?
I am afraid there is no way to functionally assign value to local scope variable. E.g.
{eval parse "a: 10";}1b
creates global variable a.
You may fix some scope name, e.g. .l, keep variables there and clear scope before function return, e.g.:
{
eval each (:),'flip (`$".l.",/:string key x;value x);
r: .l.a + .l.b + .l.c;
delete from `.l;
r
}`a`b`c!1 2 3
But getting values directly from input dictionary (like x[`a]) seems easier and clearer approach.
apply helps to simplify invoking other functions, using dictionary values as parameters. This may be what you are looking for. E.g.
{
f: {x+y+z};
f . x`a`b`c
}`a`b`c!1 2 3
I am wondering if it is possible to have a function get a variable if it is not passed explicitly.
The issue is mainly about cleaning up my code, as I have many functions that need to pass every variable that will ever be used to the next function.
In SML for example, one could easily accomplish this with something like:
fun myFun varx vary varz
let
fun otherFun () = varx
fun otherFun2 () = vary
in
otherFun() + otherFun()
end
Is there a way to allow other functions to see variables that are not explicitly passed to it? Or is this just not the way one would program in erlang?
Erlang variable scope works much in the same way:
E.g:
add_two(X) ->
F = fun(Y) ->
X + Y
end,
F(2).
Hope this helps.
I have a matrix that is outputted like this:
maximums =
[ -9.9043877608991468201413092380493, 426.34796945271797204125533010993]
[ 9.3758615553048990076305298649689, 441.87005169359418197397861057075]
But when I try and run any commands on it, I get an error saying that this matrix is still symbolic. I don't understand since it's just numeric values. Is there anyway of making this matrix outputted used by normal functions of Matlab?
To get this matrix, I did calculate derivatives of a symbolic equation and then evaluate. But I'd like to run functions on this output.
Thanks!
EDIT (Here's an example of the command/error):
[maxValue, rowIdx] = max(maximums(:,2),[],2)
Undefined function 'max' for input arguments of type 'sym'.
Since your matrix is symbolic, you have to convert it to numeric first:
maximums = double(maximums)
You have to convert it:
maximus=double(maximus)
I defined some variable in the beginning of a Mathematica notebook and used it afterwards to calculate my results. Now I want to do the same calculation several times for different values of the variable and use the results somewhere. So it would be useful to define a new function with this variable as parameter and the content of my notebook as body. But then I would have to write everything in one single input line and there would be no comfortable way to see the intermediate results.
Is there any good way to deal with this kind of situation?
To clarify what I mean, the a short example:
What I could do is something like this:
In[1] := f[variable_] := (
calculations;
many lines of calcalutions;
many lines of calcalutions;
(* some comments *);
(* finally the result... *);
result
)
And afterwards use this function:
In[1] := f[value1] + f[value2]
But if somebody is interested in the intermediate result of line 1 of function f ("calculations"), then it's necessary to copy the line somewhere else. But then you can't just remove the semicolon at the end of the line to see the line's result.
Using
lc = Notebook[{Cell[
BoxData[\(\(Pause[1]\) ;\)]
, "Input"], Cell[
BoxData[\(\(Print[\(Date[]\)]\) ;\)]
, "Input"], Cell[
BoxData[\(\(Print[
\("variable = ", variable\)] \) ;\)]
, "Input"], Cell[
BoxData[\(result = \(variable^2\)\)]
, "Input"]}, WindowSize ->
{701, 810}, WindowMargins ->
{{Automatic, 149}, {Automatic,
35}}, FrontEndVersion -> "8.0 for Microsoft Windows (64-bit) (October \
6, 2011)", StyleDefinitions ->
"Default.nb"];
Or, if you saved it under longcalc.nb into the same directory as your working notebook, then
lc = Get[FileNameJoin[{SetDirectory[NotebookDirectory[]], "longcalc.nb"}]];
Now, in your working notebook evaluate:
f[var_] := (variable = var;
NotebookEvaluate[NotebookPut[lc], InsertResults -> True]);
f[value1] + f[value2]
will do what you want.
If you do instead
f[variable_] := (
{calculations,
many lines of calcalutions,
many lines of calcalutions,
(* some comments *);
(* finally the result... *);
result}
)
then your function will return a list {ir1,ir2,...,result}, where the ir1 etc are the intermediate results. You could then assign {ir1,ir2,..,re}=f[value] whereupon re would contain the final result while the ir the intermediate results.
Does this work?
you can also do intRes={}; outside function and inside the function dump values into it. Of course this gets tricky if you use parallelization inside your function, or parallelize the whole function.
AppendTo[intRes,ir1];
AppendTo[intRes,ir2];
or
f[variable_] := Block[{output={}},
calculations;
AppendTo[output,ir1];
many lines of calcalutions;
(* some comments *);
AppendTo[output,ir2];
(* finally the result... *);
{output,result}];
and execute as {intRes,result}=f[var]; -- intRes will be a list of interrim results.
If you don't need to retain intermediate results for computation, just see them, then there are much more elegant ways to view what's happenning.
For slower functions, use Monitor[] or Dynamic[] or PrintTemporary[] or ProgressIndicator[]
Results of these outputs change and/or disappear as the function progresses.
If you want a more permanent record (let's say the function runs really fast), then use Print[] to see intermediate output.
UNLESS of course you need to use intermediate results in computation.
Would like to take an advice from TCL professionals for best practice.
Say you want to construct a list with a specific data by using a proc. Now which is the best way?
proc processList { myList } {
upvar $myList list_
#append necessary data into list_
}
proc returnList {} {
set list_ {}
#append necessary data into list_
return $list_
}
set list1 {}
processList list1
set list2 [returnList ]
Which one of this practices is recommended?
EDIT: I am sorry but I can't understand consensus (and explanation) of people who answered to this question.
I virtually always use the second method:
proc returnList {} {
set result {}
# ... accumulate the result like this ...
lappend result a b c d e
return $result
}
set lst [returnList]
There's virtually no difference in memory usage or speed, but I find it easier to think functionally. Also, in Tcl 8.5 you can do the splitting up of the result list relatively simply (if that's what you need):
set remainderList [lassign [returnList] firstValue secondValue]
With that, you'd end up with a in $firstValue, b in secondValue, and c d e in $remainderList.
The first option modify an existing list whereas the second create a new list. For a better comparison, I would add a parameter to returnList() and create a return value from that parameter.
Given that, the difference is in the way of passing parameters -- by reference or by value-- and in the memory budget needed by each operation.
There is no side effect with the second method, but it could be very consuming if the list is huge.
There is no general rule for recommending one over the other. My own rule is to start with the second way unless other constraints lead not to do so.
What would the syntax be for the equivalent of:
set lst [returnList]
If you wanted to return more than one list at once?
I thought that if you did something like this:
return [$list1 $list2]
It was supposed to return a list of lists, which you could then access with lindex. But, that doesn't appear to be exactly what it does. It really just gives you two lists back, without external curly braces grouping them into a single list. In Perl, you can do something like:
($var1, $var2) = process_that_returns_two_values;
But I don't think "set" allows that in Tcl.