How to create an array of shapes in VPython? - arrays

I'm trying to create an array of spheres in VPython, each with a manually entered position. Something like:
ball[0] = sphere(pos=vector(-1,4,9))
ball[1] = sphere(pos=vector(-2,6,6))
ball[2] = sphere(pos=vector(0,6,1))
etc. The problem is that I keep getting an error reading "IndexError: list assignment index out of range". How can I resolve this issue?

see How to declare an array in Python?.
You also might want to consider using a compound
Modified code:
# https://stackoverflow.com/questions/56461496/how-to-create-an-array-of-shapes-in-vpython
from vpython import *
# https://stackoverflow.com/questions/1514553/how-to-declare-an-array-in-python
ball=[]
ball.append(sphere(pos=vector(-1,4,9)))
ball.append(sphere(pos=vector(-2,6,6)))
ball.append(sphere(pos=vector(0,6,1)))
Result:

Related

Write into re-opend NetCDF4 file

I'm trying to write numbers into an array with an unlimited dimensions. The file I've created is structured like this :
import netCDF4 as nc4
rootgrp = nc4.Dataset("test.nc",'a',format="NETCDF4")
mgrp= rootgrp.createGroup('Flex')
mgrp.createDimension('pv',None)
mgrp.createDimension('s',4)
a = mgrp.createVariable('fill',"f8",('pv','s'))
rootgrp.close()
Now I'm trying to fill this array like this :
while i<10:
f = nc4.Dataset("test.nc",'r+',format="NETCDF4")
fgrp= f.groups['Flex']
fgrp['fill'][i][0] = i
print(fgrp['fill'][i][:])
f.groups['Flex'].variables['fill'][i][3] = i
f.close()
i=i+1
But I'm always getting a 'dimension out of bounds' error even though it's telling me that I've no dimension limit. Even if I use an array with fixed 100x4 dimension i still get the same error.
Would appreciate any kind of help.
This line is the problem:
fgrp['fill'][i][0] = i
fgrp['fill'][i] gets the ith row from the 'fill' variable. It then immediately tries to index into that row with [0], which errors out because there's nothing in that row. To solve this problem, do the indexing in one step instead:
fgrp['fill'][i, 0] = i

Matlab dot index error in array filled with objects from a class

Hi everyone i'm experiencing some trouble with my code. The goal is to add multiple objects to a class, i've created the following code for that:
array = cell(1,10);
A = matrix %specified somewhere else
for ind = 1:10
[r c] = find(A<3);
random = randi([1, size(r,1)]);
array{ind} = classname(1, r(random), c(random));
end
This block of code does everything I want it to, however, later on I add this array as property to another class and have to work with that specific array to make changes to it. And that is where is goes wrong.
function growing(thisElement)
for i = 1:length(otherClass.array)
range = range((otherClass.array(i).locationX - 2), (otherClass.array(i).locationX +2));
if otherClass.array(i).pos<20
......
end
end
end
Whereby both locationX and pos are properties of classname. I get the error Dot indexing is not supported for variables of this type. For both the range as the last if part. The error is apparently very commen error, however, I don't find any answers relating my case. Has anyone an idea to bypass the error?

Move N elements in array from back to front

I have a text file contains 2 columns, I need to select one column of them as an array
which contains 200000 and cut N elements from this array and move them from back to front.
I used the following code:
import numpy as np
import glob
files = glob.glob("input/*.txt")
for file in files:
data_file = np.loadtxt(file)
2nd_columns = data_file [:,1]
2nd_columns_array = np.array(2nd_columns)
cut = 62859 # number of elements to cut
remain_points = 2nd_columns_array[:cut]
cut_points = 2nd_columns_array[cut:]
new_array = cut_points + remain_points
It doesn't work and gave me the following error:
ValueError: operands could not be broadcast together with shapes (137141,) (62859,)
any help, please??
It doesn't work because you are trying to add values stored in both arrays and they have different shapes.
One of the ways is to use numpy.hstack:
new_array = np.hstack((2nd_columns_array[cut:], 2nd_columns_array[:cut]))
Side notes:
with your code you will reorder only 2nd column of the last file since reordering is outside of the for loop
you don't need to store cut_poinsts nor remain_points in separate variables. You can operate directly on the 2nd_columns_array
you shouldn't name variables starting from a number
A simple method for this process is numpy.roll.
new_array = np.roll(2nd_column, cut)

Need help getting perl array out of scalar context

I have a perl array I need to store in the following way:
$self->{spec}->{allImages} = #allImages;
Then I need to retrieve the contents later:
print Dumper($self->{spec}->{allImages});
This yields:
$VAR1 = 10;
(the number of items in the array).
How can I break out of scalar context and get $self->{spec}->{allImages} back as a list?
Each hash value can only be a scalar.
You must store a reference to the array:
$self->{spec}->{allImages} = \#allImages;
http://perldoc.perl.org/perlreftut.html will give you more tutorial.
You need to change the assignment:
$self->{spec}->{allImages} = \#allImages;
This creates an array-ref that you can use.

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;

Resources