如何在Mathematica中循环更改数组的值?

时间:2019-05-17 07:46:18

标签: wolfram-mathematica

我在matlab中编写了如下代码:

T= ((1-(-1)) * rand([4,4],'double') + (-1) * ones(4,4));
for i=1:4
for j=1:i
T(j,i)=TT(i,j);
end
T(i,i)=0;
end

现在,我想按以下方式在mathematica中编写此代码:

T = RandomReal[{-1, 1}, {4, 4}];
For[i = 1, i < 5, i++,
For[ j = 1, j < i, j++,
T[[j, i]] = T[[i, j]]]
T[[i, i]] = 0];

但这不起作用! 你能告诉我我的错误吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

const {flow, get, curry} = require('lodash');

const people = [
  {name: 'jenny', friends: ['jeff']},
  {name: 'frank', friends: ['jeff', 'ross']},
  {name: 'sarah', friends: []},
  {name: 'jeff', friends: ['jenny', 'frank']},
  {name: 'russ', friends: []},
  {name: 'calvin', friends: []},
  {name: 'ross', friends: ['frank']},
];
const not = i => !i;

const withFriends = i => flow(
  Boolean,
  get(i, 'friends.length'), // arity of this is 2 so might be harder to lift, is it possible tho with curry?
); // No idea what i'm doing here.


const peopleWithFriends = people.filter(withFriends);
console.log(peopleWithFriends);

const withoutFriends = flow(not, withFriends);
const peopleWithoutFriends = people.filter(withoutFriends);
console.log(peopleWithoutFriends);

enter image description here

如果必须使用SeedRandom[1234]; t = u = RandomReal[{-1, 1}, {4, 4}]; t // MatrixForm

For

它会突变For[i = 1, i < 5, i++, For[j = 1, j < i, j++, t[[j, i]] = t[[i, j]]]; t[[i, i]] = 0];

t

enter image description here

一种功能上实现此目的的方法

t // MatrixForm