Im new to lua and have a problem which i believe has an elegant solution, but i just cant make it work. I've read similar questions and answers here on stack overflow and elsewhere for hours as well as testing in online lua compiler but no real progress.
Q:
i start out with an empty table:
local vertices = {}
Now, with a for loop or similar i want to populate this table so the end result has this form:
local vertices = {
{x=-6.0, y=0.0},
{x=0.0, y=1},
}
Where the values in the entries {x=-6.0, y=0.0} are arbitrary, and the number of these {x=0.0, y=1} (coordinates) are also arbitrary.
These values are to be fetched from another table and som calculated in the for loop, but that is the next step. For now i just need help populate my empty table with x,y values in a loop.
Thanks to you all.
You can add elements to a Lua table through
the table constructor local t = { v1, v2, v3 }
assignment t[k1] = v1 t[k2] = v2
table.insert(t, 1) (for consecutive integer indices starting at 1)
rawset(t, 1, 1) if you want to avoid invoking metamethods
table.move to copy elements fro one to another table
...
For your case you can simply use assignment in a for loop.
local vertices = {}
for i = 1, 100 do
vertices[i] = NewVertex();
end
To add 100 vertices to the table.
I'm working in Google Sheets and trying to create a FILTER function that returns only the results from a second table where a pair of values exists in the first table. Here's a simplified example:
SpellsInitial (Table 1)
Level
Name
1
Heal
2
Flaming Sphere
3
Fireball
SpellsHeightened (Table 2)
Level
Name
1
Heal
2
Flaming Sphere
2
Heal
3
Fireball
3
Flaming Sphere
3
Heal
And I want to filter SpellsHeightened to return only the results that are in SpellsInitial—essentially "(Level=Level)*(Name=Name)=1".
I have a FILTER function taking a level value as input to print a list of names, but I can't seem to get the ArrayFormula part to work.
=TRANSPOSE(FILTER(SpellsHeightened_Name, A30=SpellsHeightened_Level, (SpellsHeightened_Name=SpellsInitial_Name)*(A30=SpellsInitial_Level)))
I know what I actually need on the last line is "the value on a given line in SpellsHeightened_Name" because otherwise it's the whole array, but I guess I'm struggling to identify and pass in that value using only a level value as input. I tried nesting one FILTER (to get the list of names from Heightened) inside a second FILTER (to match the names up with Initial) but could get that figured out either.
Here's the actual thing in practice.
Perhaps:
=FILTER( SpellsHeightened,
ISNUMBER( MATCH( SpellsHeightened_Level&SpellsHeightened_Name,
SpellsInitial_Level&SpellsInitial_Name, 0 ) ) )
I have the following situation: a loop (stack data) with only 1 index variable and with multiple items corresponding to the statements, as in the picture below (sorry it is Excel, but is the same as in SPSS):
stack data - cases on multiple lines, but never filling for 1 respondent all the columns
I want to reach to the following situation but without using casestovars to restructure, because that creates a lot of empty variables. I remember for older versions it was a command like Update, which was moving up the cases, to reach the following result:
reducing the cases per respondent
Like starting from this:
ID Index Q1_1 Q1_2 Q1_3 Q1_4 Q1_5 Q1_6
1 1 1 1
1 2 1 1
1 3 1 1
To reach to this:
ID Q1_1 Q1_2 Q1_3 Q1_4 Q1_5 Q1_6
1 1 1 1 1 1 1
But without using casestovars. Is there any command in SPSS syntax for this?
Thank you very much, have a nice day!
Not entirely sure how variable your data structure is likely to be in reality but if as demo'ed where you have only a single response for each q1_1 to q1_6 per respondent ID, then the below would be sufficient:
dataset declare dsAgg.
aggregate outfile="dsAgg" /break=respid /q1_1 to q1_6=max(q1_1 to q1_6).
Also not sure of the significance of duplicate index values within the same respondent IDs, if this was intended or not.
The following syntax could do the job -
* first we'll recreate your example data.
data list list/respid index q1_1 to q1_6.
begin data
1,1,1,,,,,
1,2,,2,,,,
1,3,,,1,,,
1,4,,,,2,,
1,5,,,,,1,
1,6,,,,,,2
2,1,3,,,,,
2,1,,4,,,,
2,2,,,5,,,
2,2,,,,4,,
2,3,,,,,3,
2,3,,,,,,2
end data.
* now to work: first thing is to make sure the data from each ID are together.
sort cases by respid index.
* the loop will fill down the data to the last line of each ID.
do repeat qq=q1_1 to q1_6.
if respid=lag(respid) and missing(qq) qq=lag(qq).
end repeat.
* the following lines will help recognize the last line for each ID and select it.
compute lineNR=$casenum.
aggregate /outfile=* mode=ADDVARIABLES/break=respid/MXlineNR=max(lineNR).
select if lineNR=MXlineNR.
exe.
I have data consisting of n key-value pairs in a table, A
I'd like to produce another table, B of size (n+1) x (n+1), where the first row/column are the keys of the original table, and entry (i,j) is some function of the ith and jth value
Ex:
A:
K|V
---
a 1
b 2
c 3
B:
a b c
a f(1,1) f(1,2) f(1,3)
b f(2,1) f(2,2) f(2,3)
c f(3,1) f(3,2) f(3,3)
Depends on the function you need. Assuming B2:B4 contains {1,2,3} , The following can be done. Each will use different functions and will add or subtract in different ways. The first one is the only one, that'll work in the exact way you asked, but that's just for Matrix multiplication - You could maybe use that as base and do other functions on it as needed.
=ARRAYFORMULA(MMULT(--(B2:B4), --TRANSPOSE(B2:B4)))
=ARRAYFORMULA((--(B2:B4)/--TRANSPOSE(B2:B4)))
=ARRAYFORMULA((--(B2:B4)+--TRANSPOSE(B2:B4)))
=ARRAYFORMULA((--(B2:B4)---TRANSPOSE(B2:B4)))
I have 3 tables PostText, PostImage and PostVideo.
Now I am combining data from all the above three table into a single array called userposts.
Now from userposts I want to access only 10 records starting with offset 15.
How can I do that?
I tried out userposts.first(10). It gives me first 10 records but I want 10 records starting from offset-15.
thanks in advance.
userposts.drop(15).first(10) will help you
You should use ary[start, length] → new_ary or nil method.
..returns a subarray starting at the start index and continuing for length elements,
userposts[10, 15]