如何将结果放入mathematica中的表或数组中?
你好 我有一个数字列表,例如 k_1、k_2、...k_n,而 f 是一个函数。 现在我将 f 应用于数字列表,并且我需要这些数字使得 f 不断增加,即
f(k_i)>f(k_j) for any i>j .
我可以在不同的行中获取结果数字 k_i,但我需要在一个表中用逗号或其他内容分隔结果并计算结果数量。
例如:
k = Table[k1, k2, k3, k4, k5, k6, k7, k8, k9, k10];
count = 0;
i=1;
For[j = i, j <= 10, j++,
If[f[k[[j]]] - f[k[[i]]] > 0, i = j; Print["k", i];
count = count + 1]];
Print["count= ", count]
我得到的结果如下:
k2
k3
k5
k9
count=4
但我需要将结果放在一起:
{k2,k3,k5,k9}
count=4
有什么想法吗?
谢谢
Hi
I have a list of numbers for example k_1,k_2,...k_n, and f is a function.
Now I apply f on the list of numbers and I need those numbers such that f is increasing,i.e.
f(k_i)>f(k_j) for any i>j .
I can get the results number k_i's each in different line, but I need the results in one table separated with comma or something else and counting the number of results.
For example:
k = Table[k1, k2, k3, k4, k5, k6, k7, k8, k9, k10];
count = 0;
i=1;
For[j = i, j <= 10, j++,
If[f[k[[j]]] - f[k[[i]]] > 0, i = j; Print["k", i];
count = count + 1]];
Print["count= ", count]
I got the result like:
k2
k3
k5
k9
count=4
but I need the results to be together:
{k2,k3,k5,k9}
count=4
any idea?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了
Print
之外,您还可以执行AppendTo
,即list={};AppendTo[list,5]
开始学习函数式可能会很好编程方法,因为 Mathematica 有工具可以提高其效率,上面的代码可能看起来像这样
Instead of
Print
, you could doAppendTo
, ielist={};AppendTo[list,5]
It might be good to start learning functional programming approach as Mathematica has tools to make it efficient, your above code might look something like this
您似乎想要最长的递增子序列。据我所知,在 Mathematica 中获取它的最简单、最有效的方法如下:
示例:
原则上,答案不是唯一的 - 可能有几个不同的具有相同长度的最长递增子序列。
You seem to want the longest increasing subsequence. The simplest and most efficient way I am aware of to get it in Mathematica is the following:
Example:
In principle, the answer is not unique - there may be several different longest increasing subsequences with the same length.