1. Need help combining 2 sequences

I have 2 sequences such as this:

voters = {{82,68,82,68,82},{82,68,68,68,82},{82,68,68,68,68}} 
colors = {{23,71,23,71,23},{23,71,71,71,23},{23,71,71,71,71}} 

and I would like to combine them into 1 sequence like this

image = {{82,23,68,71,82,23,68,71,82,23},{82,23,68,71,68,71,68,71,82,23},{82,23,68,71,68,71,68,71,68,71}} 

so that I can use

display_text_image({1,1}, image) 

to display the image quickly.

I would like to do this without having to resort to a nested for loop but I can't find any sequence routine that will do what I want. I might not be understanding the supplied sequence routines.

TIA,
Lonny

new topic     » topic index » view message » categorize

2. Re: Need help combining 2 sequences

Lnettnay said...

I have 2 sequences such as this:

voters = {{82,68,82,68,82},{82,68,68,68,82},{82,68,68,68,68}} 
colors = {{23,71,23,71,23},{23,71,71,71,23},{23,71,71,71,71}} 

and I would like to combine them into 1 sequence like this

image = {{82,23,68,71,82,23,68,71,82,23},{82,23,68,71,68,71,68,71,82,23},{82,23,68,71,68,71,68,71,68,71}} 

so that I can use

display_text_image({1,1}, image) 

to display the image quickly.

I would like to do this without having to resort to a nested for loop but I can't find any sequence routine that will do what I want. I might not be understanding the supplied sequence routines.

Here's what I came up with...

include "std/math.e" 
 
function merge( sequence a, sequence b ) 
 
    integer max_i = min({ length(a), length(b) }) 
    sequence c = repeat( {}, max_i ) 
     
    for i = 1 to max_i do 
         
        integer max_j = min({ length(a[i]), length(b[i]) }) 
        c[i] = repeat( 0, max_j * 2 ) 
         
        for j = 1 to max_j do 
             
            integer j2 = j * 2 
            c[i][j2-1..j2] = {a[i][j], b[i][j]} 
             
        end for 
         
    end for 
     
    return c 
end function 

-Greg

new topic     » goto parent     » topic index » view message » categorize

3. Re: Need help combining 2 sequences

Lnettnay said...

I have 2 sequences such as this:

voters = {{82,68,82,68,82},{82,68,68,68,82},{82,68,68,68,68}} 
colors = {{23,71,23,71,23},{23,71,71,71,23},{23,71,71,71,71}} 

and I would like to combine them into 1 sequence like this

image = {{82,23,68,71,82,23,68,71,82,23},{82,23,68,71,68,71,68,71,82,23},{82,23,68,71,68,71,68,71,68,71}} 

so that I can use

display_text_image({1,1}, image) 

to display the image quickly.

I would like to do this without having to resort to a nested for loop but I can't find any sequence routine that will do what I want. I might not be understanding the supplied sequence routines.

TIA,
Lonny

-- If your two sequences are raw data in the program,  
-- it would be most appropriate to have them as 
voters = {{82,0,68, 0,82, 0,68, 0,82,0},{82, 0,68, 0,68, 0,68, 0,82, 0},{82, 0,68, 0,68, 0,68, 0,68, 0}}  
 
colors = {{0,23, 0,71, 0,23, 0,71, 0,23,},{ 0,23, 0,71, 0,71, 0,71, 0,23},{0,23, 0,71, 0,71, 0,71, 0,71}}               
        
-- Even if the sequences are program generated, you could interpose  
-- the zeros in the program that generates the sequences. 
-- Then an addition of the sequences would give you your required sequence. 
 
new topic     » goto parent     » topic index » view message » categorize

4. Re: Need help combining 2 sequences

Hi Greg,

Since the 2 sequences are exactly the same size I could avoid all the testing and your program degenerates to a nested for loop that I'm trying to avoid.

Thanks anyway,
Lonny

PS: I think I'm going to have to rethink the method I use to generate the sequence. Since it's only done once at the beginning of the program it might be a case of over optimizing. I'm rewriting a program I did years ago that I lost to a hard drive crash. I thought I did it this way but I don't remember all these difficulties.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Need help combining 2 sequences

EUWX said...
-- If your two sequences are raw data in the program,  
-- it would be most appropriate to have them as 
voters = {{82,0, 68,0, 82,0, 68,0, 82,0},{82,0, 68,0, 68,0, 68,0, 82,0},{82,0, 68,0, 68,0, 68,0, 68,0}}  
 
colors = {{0,23, 0,71, 0,23, 0,71, 0,23},{ 0,23, 0,71, 0,71, 0,71, 0,23},{0,23, 0,71, 0,71, 0,71, 0,71}}               
        
-- Even if the sequences are program generated, you could interpose  
-- the zeros in the program that generates the sequences. 
-- Then an addition of the sequences would give you your required sequence. 
 

Hi EUWX,

I'm using the sequence operations like so:

voters = repeat(repeat(2,dimensions[2]), dimensions[1]) 
-- 2d array of 2s 
voters = rand(voters) - 1                                                    
-- 2d array of 0s and 1s 
colors = voters 
voters = 'D' + voters * ('R' - 'D')                                          
-- 0s become 'D's and 1s become 'R's 
colors = 23 + colors * (48)                                                 
-- 0s become 23s and 1s become 71s 
 
-- to generate a 2d sequence of 'D's and 'R's 
-- the way I'm doing it doesn't allow me to put in extra 0s 

Thanks for your suggestion. As I told Greg in a previous post since I only generate the sequence once I guess I'll just go with the nested for loops.
Lonny

new topic     » goto parent     » topic index » view message » categorize

6. Re: Need help combining 2 sequences

Lnettnay said...

the way I'm doing it doesn't allow me to put in extra 0s </eucode>

The way you are doing it is most amenable to initially generating sequences of twice the size and multiplying by a sequence of
1,0,1,0,1,0..... of appropriate length and shape and a sequence of 0,1,0,1,0,1..... of similar length
That way you will have generated the two sequences with a length equal to the final size you want,
and add the two sequences as I suggested.

that is, if you do not wish to loop as you hinted at in your first post.

new topic     » goto parent     » topic index » view message » categorize

7. Re: Need help combining 2 sequences

Thanks EUWX,

That looks like it should work. I'm at work now. I'll test out when I get home.

Lonny

new topic     » goto parent     » topic index » view message » categorize

8. Re: Need help combining 2 sequences

Hi EUWX,

Well I tried your idea and it worked (sort of). The problem is that I want each 82 to be followed by a 23 and each 68 to be followed by a 71. (Actually those 2 numbers are wrong. There seems to be some flakiness in the console color routines.)

Thanks for trying,
Lonny

new topic     » goto parent     » topic index » view message » categorize

9. Re: Need help combining 2 sequences

include std/math.e 
-- This functions interleaves elements from two sequences to form a new sequence. 
-- For example, 
--    interleave("abcdef", "123456") --> "a1b2c3d4e5f6" 
public function interleave(sequence A, sequence B) 
	sequence res 
	integer j 
 
	-- Set resulting sequence length. Will always be combined length of both arguments.	 
	res = repeat(0, length(A) + length(B)) 
	-- Initialize target index in result sequence. 
	j = 0 
	 
	-- First, we interleave elements from both arguments, up to the 
	-- last element in the shortest argument. 
	for i = 1 to math:min({length(A), length(B)}) do 
		j += 1 
		res[j] = A[i] 
		j += 1 
		res[j] = B[i] 
	end for 
	 
	-- Next, we simply copy the remaining elements from the longest argument. 
	j += 1 
	if length(A) < length(B) then 
		res[j .. $] = B[length(A) + 1 .. $] 
		 
	elsif length(A) > length(B) then 
		res[j .. $] = A[length(B) + 1 .. $] 
		 
	end if 
	 
	-- And we're done.				 
	return res 
end function 
 
sequence image 
sequence voters 
sequence colors 
 
voters = {{82,68,82,68,82},{82,68,68,68,82},{82,68,68,68,68}}  
colors = {{23,71,23,71,23},{23,71,71,71,23},{23,71,71,71,71}}  
image = {} 
 
for i = 1 to length(voters) do 
	image = append(image, interleave(voters[i], colors[i])) 
end for 
 
? image 
new topic     » goto parent     » topic index » view message » categorize

10. Re: Need help combining 2 sequences

Hi Derek,

That looks kind of like Greg's merge function in post #2.

Because I'm only generating the sequence once I can use a nested for loop.

I also figured out another way to do it - instead of generating 'D's and 'R's to represent Democrats and Republicans I'm just going to let the color represent the party.

BTW: Who decided that Republicans are Red? It seems to me that the Democrats motto of take from those that have and give to those that don't have is a lot like "From each according to his ability, to each according to his need" which I learned as the Communist motto way back when. smile

Thanks for the help,
Lonny

new topic     » goto parent     » topic index » view message » categorize

11. OT: Re: Need help combining 2 sequences

Lnettnay said...

BTW: Who decided that Republicans are Red?

It's hard to pinpoint a specific person or organization, AFAICT, but...

http://en.wikipedia.org/wiki/Red_states_and_blue_states

It does make a lot of sense to paint the democrats red (the traditional liberal colour) and republicans blue (the tradiational conservative colour), but this is only slightly more likely to happen than getting the US to adopt metric over imperial.

Lnettnay said...

It seems to me that the Democrats motto of take from those that have

That's not a motto, that's taxation!

Seriously, I can't find an official motto or slogan of the democratic party. The closest thing I can find to that are the various campaign slogans used in the presidential elections.

Slightly offhand, see how well a goverment without taxation worked for the US under the Articles of Confederation.

Lnettnay said...

and give to those that don't have is a lot like "From each according to his ability, to each according to his need" which I learned as the Communist motto way back when. smile

Thanks for the help,
Lonny

If only I could invoke Godwin's law here. So close...

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu