2d array in Haskell - arrays
I'm learning how to use arrays in Haskell, for example, generating a times table:
Prelude Data.Array> array ((0,0),(10,12)) [((x,y),x*y) | x<-[0..10], y<-[0..12]]
array ((0,0),(10,12)) [((0,0),0),((0,1),0),((0,2),0),((0,3),0),((0,4),0),((0,5),0),((0,6),0),((0,7),0),((0,8),0),((0,9),0),((0,10),0),((0,11),0),((0,12),0),((1,0),0),((1,1),1),((1,2),2),((1,3),3),((1,4),4),((1,5),5),((1,6),6),((1,7),7),((1,8),8),((1,9),9),((1,10),10),((1,11),11),((1,12),12),((2,0),0),((2,1),2),((2,2),4),((2,3),6),((2,4),8),((2,5),10),((2,6),12),((2,7),14),((2,8),16),((2,9),18),((2,10),20),((2,11),22),((2,12),24),((3,0),0),((3,1),3),((3,2),6),((3,3),9),((3,4),12),((3,5),15),((3,6),18),((3,7),21),((3,8),24),((3,9),27),((3,10),30),((3,11),33),((3,12),36),((4,0),0),((4,1),4),((4,2),8),((4,3),12),((4,4),16),((4,5),20),((4,6),24),((4,7),28),((4,8),32),((4,9),36),((4,10),40),((4,11),44),((4,12),48),((5,0),0),((5,1),5),((5,2),10),((5,3),15),((5,4),20),((5,5),25),((5,6),30),((5,7),35),((5,8),40),((5,9),45),((5,10),50),((5,11),55),((5,12),60),((6,0),0),((6,1),6),((6,2),12),((6,3),18),((6,4),24),((6,5),30),((6,6),36),((6,7),42),((6,8),48),((6,9),54),((6,10),60),((6,11),66),((6,12),72),((7,0),0),((7,1),7),((7,2),14),((7,3),21),((7,4),28),((7,5),35),((7,6),42),((7,7),49),((7,8),56),((7,9),63),((7,10),70),((7,11),77),((7,12),84),((8,0),0),((8,1),8),((8,2),16),((8,3),24),((8,4),32),((8,5),40),((8,6),48),((8,7),56),((8,8),64),((8,9),72),((8,10),80),((8,11),88),((8,12),96),((9,0),0),((9,1),9),((9,2),18),((9,3),27),((9,4),36),((9,5),45),((9,6),54),((9,7),63),((9,8),72),((9,9),81),((9,10),90),((9,11),99),((9,12),108),((10,0),0),((10,1),10),((10,2),20),((10,3),30),((10,4),40),((10,5),50),((10,6),60),((10,7),70),((10,8),80),((10,9),90),((10,10),100),((10,11),110),((10,12),120)]
I'm wondering if this is the correct way to hold a matrix or 2d-array of values? Why does it give a list of ((x,y),value) instead of giving a table of values? Is there a way to change how it prints the array?
Using a tuple as the index is the correct way of getting a multidimensional array. If you want to print it out differently, you'll have to write your own function to convert it to a string.
For example, you could have something like this:
showTable arr =
unlines $ map (unwords . map (show . (arr !))) indices
where indices = [[(x, y) | x <- [startX..endX]] | y <- [startY..endY]]
((startX, startY), (endX, endY)) = bounds arr
That's just the Show instance. The Array constructor is not exported from Data.Array, so you can't directly construct an array. The Show instance produces valid Haskell code that can be used to construct the array from the list of its associations.
Related
Fill in numpy array using fancy indexing
Suppose I have an say NxM called Image, and I have 3 1xK arrays, x_array, y_array, z_array where x_array and y_array represent index values and z_array represents value to insert, ex: Image[y_array[0], x_array[0]] = z_array[0] What is the best way to do this?
You can directly index using the arrays: Image[x_array, y_array] = z_array
Julia: 2d array assignment
I'm trying to do the assignment in the 2d array with the nested loop. I'm trying to access the elements of the array as follows. But I get a mistake. I've searched, but I didn't get results. How can I assign Julia in the 2d array? for x in 1:total for y in 1:W #show (x, y) if agirliklar[x] <= y V[x][y] = getMax(V[x-1][y], degerler[x] + V[x-1][y - agirliklar[x]]) else print("sa") V[x][y] = V[x-1][y] end end end BoundsError: attempt to access 7×6 Array{Int64,2} at index [0] My code Error
In Julia arrays are 1-based not 0-based. You try to access V[x-1] where x can take value of 1. Site note: always provide a minimum working example (MWE) rather than just dumping a part of your production code.
(At least) two things are wrong here: As #PrzemyslawSzufel says, ordinary Julia arrays are 1-indexed, so you cannot access them at index zero. Though it is possible to get special arrays that are 0-indexed. If V is a 2D array, as you are saying, you cannot access it like this: V[x][y]. Instead you access them like this: V[x, y]. You can read more about this here: https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1
Arrays in matlab
I have a structure names eye_record which has 6 fields, one of which is x_pos_measured_deg:[1800x1 double] I want to declare an array in such a way that using for loop, i can get all values of that specific field into a new array and do some work on them. Can anyone show me how to do that? here is m code: arr=zeros(1,1800); for t=1:length(eye_record); arr(t)= eye_record(t).x_pos_measured_deg; end it gives me this error: In an assignment A(I) = B, the number of elements in B and I must be the same. How can i fix this? or how should i declare my array so that it won't give me this error? I want all the objects or values, which are in x_pos_measured_deg field to go into my new array.
Your eye_record is struct, not array, so you can not use indexing with eye_record. Your eye_record.x_pos_measured_deg is array and you have to loop through it. So the loop should be: arr=zeros(1,1800); for t=1:length(eye_record.x_pos_measured_deg) arr(t)= eye_record.x_pos_measured_deg(t); end But actually, you can assign values directly like: arr=zeros(1,1800); arr = eye_record.x_pos_measured_deg'; since you declared arr to have size of 1x1800, and eye_record.x_pos_measured_deg has size of 1800x1. Without arr=zeros(1,1800);, then no ' at the end: arr = eye_record.x_pos_measured_deg;
R populate multidimensional array
Hi I am stuck with one of these simple but time-consuming errors: How can I populate an array with loops? I know I am on a C approach here and R isn't C. Data <-[SOMETHING HERE] One <-200 Two <-100 array222 <- array(0,length(SomeLength)) for (i in 1:One) { for (j in 1:Two) { array222[i][j] = sample(Data,1) } I want to populate the array with random samples from another dataset but all I get is this: Warning in array222[i][j] = sample(Data, 1) : number of items to replace is not a multiple of replacement length
First of all, you wouldn't use loops to do this in R. You'd just do array222 <- matrix(sample(Data, One*Two, replace=T), nrow=One, ncol=Two) But going back to your code, you fail to properly initialize your array222 variable. The matrix() syntax is probably easier for a 2-D array, but you could also use array(0, dim=c(One,Two)). You need to create it with the proper dimensions. And additionally, the proper way to index a dimensional array is array222[i,j] #NOT array222[i][j]
how to get array of properties from array of objects in matlab
I am using an array of objects in my program, and each object has several attributes. I want to be able to extract separate arrays from this array of objects, one array for each attribute. here is a snippet of the relevant code: dailyDataMatrix(m,n)=dailyData('',''); %creates an mxn array of dailyData objects for i=1:m for j=1:n dailyDataMatrix(i,j)=dailyData(datainput1, datainput2)%dailyData constructor, sets attributes end end dailyDataMatrix.attribute But when I try to access a certain attribute as in the code above, say of type string, I get a strange result. Instead of getting an array of strings, I get something else. When I try to print it, rather than printing an array, it prints a series of individual values ans = 'string1' ans = 'string2' ... When I try to call size(dailyDataMatrix.attribute) className = class(dailyDataMatrix.attribute) I get "error using size: too many input arguments" and "error using class: The CLASS function must be called from a class constructor." However, when I write this as thing=dailyDataMatrix.attribute className = class(thing) size(thing) I get the response classname = 'double' and size = 1x1. Why is this not returning an array the same size as dailyDataMatrix? And an aside question is why the two different ways of writing the code above give different results? and how can I get the result that I want? Thanks, Paul
You can capture all the outputs using a cell array or using square brackets if the types are same. For regular array when all values are of same type use, thing=[dailyDataMatrix.attribute]; If the types are different you can use thing = cell(1,N); % N is the number of elements in dailyDataMatrix [thing{:}] = dailyDataMatrix.attribute;