0
votes

I have a set of around 500 (x,y,z) real values. Since I will need to bin the values based on their (x,y) coordinates, I stripped the z values and stored in on a seperate list. I am left with only the x,y values; I rescaled and rounded them to index pairs in the range of, 1..100 range.

Now I want to populate an array with the z values in a 100x100 matrix at the particular (x,y) coordinates.

More precisely,

I have a set of values for example : data = {{2.62399, 0.338057, 2.09629}, {1.8424, 0.135817, 3.21925}, {0.702257, 1.14502, 3.9335}...

I stripped it of its zvalues and store it in zvalues list:

zvalues = {2.09629, 3.21925, 3.9335....

I rounded, rescaled and created a new array of indices

indices = {{53, 7}, {37, 3}, {14, 23}...

I want to create a new 100x100 matrix and place the zvalues on the coordinates corresponding to the indices matrix

For example, in pseudocode

For (int i = 1, i < 101, i++){

NewArray(indices[i]) = zvalues[i];
}

The first time the loop will run, it should do NewArray(53,7) = 2.09629.

I want to know the syntax to loop through the indices array and populate the 2 dimensional 100x100 NewArray with zvalues

2

2 Answers

0
votes

to follow your basic approach you need to initialize the array:

newArray=Table[,{100},{100}]

then in the loop the syntax is:

newArray[[indices[[i,1]],indices[[i,2]]]]=zdata[[i]]

note the double square brackets for referencing parts of arrays (or lists in Mathematica terminology)

A better approach would be to create a SparseArray, which for one thing would not require pre-initialization, or even knowing the dimensions in advance.

Finally in mathematica you can usually use an object oriented approach, avioding the "do" loop all together:

data = {{1.5, 1.1, 1.1}, {2.2, 2.2, 2.2}, {1.01, 2.3, 1.2}};
m1 = Table[, {2}, {2}];
(m1[[Floor[#[[1]]], Floor[#[[2]]]]] = #[[3]]) & /@ data;
m1
m2 = SparseArray[ Floor[#[[1 ;; 2]]] -> #[[3]] & /@ data , Automatic,];
Normal[m2]


{{1.1, 1.2}, {Null, 2.2}}
{{1.1, 1.2}, {Null, 2.2}}
0
votes

While I don't understand why you want to create a new way of indexing your array, this will do what you want :

data = {{2.62399, 0.338057, 2.09629}, {1.8424, 0.135817, 3.21925}, {0.702257, 1.14502, 3.9335}};
zvalues = {2.09629, 3.21925, 3.9335};
indices = {{53, 7}, {37, 3}, {14, 23}};

newArray[xIndex_, yIndex_]:=Take[data, Position[indices, {xIndex, yIndex}][[1, 1]]][[1, 3]]

newArray[53, 7]
(* 2.09629 *)