I'm investigating matrix assembly and one of the things I'm trying to do is add specific values to a larger matrix based on a list which is essentially defining connectivity of nodes.
For example: Start with a null matrix, for this example a 6x6 will be selected.
m=ConstantArray[0, {6, 6}]
If I want to add 2 to the value at position {3,4} I can use the following code.
m=ReplacePart[m, {3, 4} -> (Part[m, 3, 4] + 2)]
What I would like to do is feed this process via lists though. Creating a list of values to be added.
v={{2},{3},{4}}
Creating a list of locations to add these values.
locs = {{1, 2},{1,3},{1,4}}
Combining these two lists so that it gives {value, loc} as a new list
comb = Partition[Flatten[Riffle[v, locs]], 3]
Trying to map this new list comb across the matrix m using ReplacePart
f = ReplacePart[m,{#[[2]], #[[3]]} -> (Part[m, #[[2]], #[[3]]] + #[[1]])] & /@ comb
If my feeder list (comb) only have a single entry, the code runs, but I get an extra level added to m, which I can Flatten out afterwards.
EDIT Values may be added to the same location several times and so methods need to keep a running total.
However, once I get two or more entries to my feeder list I get a new matrix for each iteration, which makes sense but not what I want.
I'm missing something here as it's creating a new entry for each time the list is mapped across. When the intention is to add the values in the list to the existing values at a certain position and return the new updated matrix.
I'm guessing to get a single matrix with all of the values added to the exising values will need an approach that writes the new matrix to m before running the next element in the list. Perhaps using something similar to a Do or maybe Module (which are a complete mystery to me).
I'm hoping my code and description is clear and that I'm not too far away from a working solution.
Any help gratefully appreciated.
SparseArray
to a standard array (nestedList
) usingNormal
? $\endgroup$