Mathematica:如何将函数应用于表的某一列 [英] Mathematica: How to apply function to a certain column of a table

查看:29
本文介绍了Mathematica:如何将函数应用于表的某一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将函数应用于表的特定列.说到 (m x n) 表的第 i 列.其实我只是想将该列中的所有元素乘以一个标量,但是应用一个通用函数也可以.

I would like to apply a function to a specific column of a table. Say to the i-th column of a (m x n) table. Actually I just want to multiply all elements in that column with a scalar, but the application of a general function would be fine as well.

它可能只需要一些 Map 或 MapAt 命令,可能与 Transpose 结合使用以应用于行而不是列 - 但我无法找出寻址整个列(或行)的正确语法..

It probably just needs some Map or MapAt command, maybe combined with a Transpose in order to apply to rows instead of columns - but I can't figure out the correct syntax for addressing an entire column (or row)..

任何提示将不胜感激.

推荐答案

例如

ranfunc=Function[{f,mat, n},Transpose[MapAt[f /@ # &, Transpose[mat], n]]]

将 f 应用于 mat 的第 n 列的每个元素.因此,例如,

will apply f to each element of the nth column of mat. So, for instance,

ranfunc[Sin[Cos[#]] &, {{1, 2, 3}, {a, b, c}, {\[Alpha], \[Beta], \[Gamma]}}, 2]

将应用 Sin[Cos[#]]&到第二列的每个元素,而

will apply Sin[Cos[#]]& to each element of the second column, while

ranfunc[s*# &, {{1, 2, 3}, {a, b, c}, {\[Alpha], \[Beta], \[Gamma]}},2]

将第二列上的每个元素乘以 s

will multiply each element on the second column by s

这篇关于Mathematica:如何将函数应用于表的某一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆