Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Building a specialized Part function...' topicStudent Support Forum > General > "Building a specialized Part function..."

Next Comment >Help | Reply To Topic
Author Comment/Response
Mats
11/27/06 9:58pm

Hello.


I'd like to iteratively build up a matrix from a list of assignments: {row,col}->val

The problem however is that the Part function won't handle lists...

What's the most elegant way to do this?


One method I thought of was to implement a function like so:

FoldPart[tensor_, list_] :=
Apply[Part, Join[{tensor}, list]]

But it gives an error when I try to do things like this:

FoldPart[{{1,2},{3,4}},{2,2}] = 5


A second method I thought of using a SparseArray and converting to a regular array, but I'm concerned about efficiency (e.g. Mathematica not hashing the patterns and giving O(log(n)) time rather than O(1) time per lookup).


While the second method is fine for my computations, I'd just like to know what the most elegant way to do this would be in general, since it might be important when writing more complex pieces of code...


Thank you,
Mats

URL: ,

Subject (listing for 'Building a specialized Part function...')
Author Date Posted
Building a specialized Part function... Mats 11/27/06 9:58pm
Re: Building a specialized Part function... yehuda ben-s... 11/29/06 08:55am
Re: Building a specialized Part function... Mats 11/30/06 01:22am
Re: Building a specialized Part function... Forum Modera... 11/30/06 08:29am
Re: Building a specialized Part function... Mats 11/30/06 09:33am
Next Comment >Help | Reply To Topic