What I am trying to do is combine two postscript files into a pdf. I have already tried using ghostscript to combine them however the issue with that is ghostscript produces a pdf with one ps file on one page and the other on page two which is not what i wish to accomplish
%!PS-Adobe-3.0
%%Pages: 1
%%DocumentData: Clean7Bit
%%LanguageLevel: 2
%%EndComments
%%Page: 1 1
save
6 dict begin currentpagedevice/PageSize get dup 0 get
0 sub 0 sub/w exch
def 1 get 0 sub 0 sub/h exch
def/x 87 def/y 87 def 0 0/b y h mul x
w mul gt def b{w y}{h x}ifelse div/c x h mul y w mul gt def c{w x}{h y}ifelse
div gt{h add translate -90 rotate b{w y h x w mul y div sub 2 div 0}{h
x 0 w y h mul x div sub 2 div}}{translate c{w x 0 h y w mul x div sub 2 div}{h
y w x h mul y div sub 2 div 0}}ifelse ifelse translate div dup scale
end 9 dict begin
{/T currentfile/ASCII85Decode filter def/DeviceGray setcolorspace
/F T/LZWDecode filter def
<</ImageType 1/Width 87/Height 87/BitsPerComponent
1/ImageMatrix[1 0 0 -1 0 87]/Decode
[0 1]/DataSource F>> image
F closefile T closefile}
%%BeginData:;
exec
J3Vsg3$\8+#CjR`&3*WA'+TR\#<!\`;#aOp`$+XV`%h8\!!!K[!9Aet#*;BOP%d==7jS.,hsVBYnDH
N[+5cZ=UtTfLSkWhgRG8\?,WGcN^j5#cP]3KsWRSLmE;_bp<GM2!Q!1FiRE6!+E91]DA$X`8iQFZYB
0mS8-o5Xu!f_0094V'pA:#g3R,3i_$'Yp\)8e-_&1fqhs%"+h!UL4I6R#tNA[6Bt^^UAX[:aRMSIU!
416=^MmJq2-+0!fVFnDer\kkA9UY#Q*n/l#Ih/>7,IEq;.)e7/q%Q&SL"j'""q7'ad9:Z00;\?qF/.
8a*mIt,hY3"qji0!BA*e-tRs6.F-k:_SnL*&QCrBB*lce!!;K8Fj4Hp6d?"Tg3Y4:8BLi#B-/VXSPS
#XP\B&qLYW4;Koo&C`>f_omCk&%aL+C?WTr*(:Q0YB"7Y0#*i?(aUW\?B(Z"l6;3X;8O\%8-n:/*6;
f^!W~>
%%EndData
end restore showpage
%%Trailer
%%EOF
Here is the second file
The file is to big here is a link to it http://pastebin.com/R0Kgarem
Not sure how I would go about solving this problem. I need the 2 ps files under each other without any white space.
Assuming you mean one toward the top of the page and the other toward the bottom of the page (a 2-up), you just need to prevent the first file from executing showpage (by redefining, or editing to remove the word showpage), and then translate (0 -y translate, where y is the height of the first image) before executing the second file. This assumes the first image is already positioned at the top of the page, otherwise you need to translate it to the correct position first.
Related
Say we have a matrix of zeros and ones
0 1 1 1 0 0 0
1 1 1 1 0 1 1
0 0 1 0 0 1 0
0 1 1 0 1 1 1
0 0 0 0 0 0 1
0 0 0 0 0 0 1
and we want to find all the submatrices (we just need the row indices and column indices of the corners) with these properties:
contain at least L ones and L zeros
contain max H elements
i.e. take the previous matrix with L=1 and H=5, the submatrix 1 2 1 4 (row indices 1 2 and column indices 1 4)
0 1 1 1
1 1 1 1
satisfies the property 1 but has 8 elements (bigger than 5) so it is not good;
the matrix 4 5 1 2
0 1
0 0
is good because satisfies both the properties.
The objective is then to find all the submatrices with min area 2*L, max area H and containg at least L ones and L zeros.
If we consider a matrix as a rectangle it is easy to find all the possibile subrectangles with max area H and min area 2*L by looking at the divisors of all the numbers from H to 2*L.
For example, with H=5 and L=1 all the possibile subrectangles/submatrices are given by the divisors of
H=5 -> divisors [1 5] -> possibile rectangles of area 5 are 1x5 and 5x1
4 -> divisors [1 2 4] -> possibile rectangles of area 4 are 1x4 4x1 and 2x2
3 -> divisors [1 3] -> possibile rectangles of area 3 are 3x1 and 1x3
2*L=2 -> divisors [1 2] -> possibile rectangles of area 2 are 2x1 and 1x2
I wrote this code, which, for each number finds its divisors and cycles over them to find the submatrices. To find the submatrices it does this: take for example a 1x5 submatrix, what the code does is to fix the first line of the matrix and move step by step (along all the columns of the matrix) the submatrix from the left edge of the matrix to the right edge of the matrix, then the code fixes the second row of the matrix and moves the submatrix along all the columns from left to right, and so on until it arrives at the last row.
It does this for all the 1x5 submatrices, then it considers the 5x1 submatrices, then the 1x4, then the 4x1, then the 2x2, etc.
The code do the job in 2 seconds (it finds all the submatrices) but for big matrices, i.e. 200x200, a lot of minutes are needed to find all the submatrices. So I wonder if there are more efficient ways to do the job, and eventually which is the most efficient.
This is my code:
clc;clear all;close all
%% INPUT
P= [0 1 1 1 0 0 0 ;
1 1 1 1 0 1 1 ;
0 0 1 0 0 1 0 ;
0 1 1 0 1 1 1 ;
0 0 0 0 0 0 1 ;
0 0 0 0 0 0 1];
L=1; % a submatrix has to containg at least L ones and L zeros
H=5; % max area of a submatrix
[R,C]=size(P); % rows and columns of P
sub=zeros(1,6); % initializing the matrix containing the indexes of each submatrix (columns 1-4), their area (5) and the counter (6)
counter=1; % no. of submatrices found
%% FIND ALL RECTANGLES OF AREA >= 2*L & <= H
%
% idea: all rectangles of a certain area can be found using the area's divisors
% e.g. divisors(6)=[1 2 3 6] -> rectangles: 1x6 6x1 2x3 and 3x2
tic
for sH = H:-1:2*L % find rectangles of area H, H-1, ..., 2*L
div_sH=divisors(sH); % find all divisors of sH
disp(['_______AREA ', num2str(sH), '_______'])
for i = 1:round(length(div_sH)/2) % cycle over all couples of divisors
div_small=div_sH(i);
div_big=div_sH(end-i+1);
if div_small <= R && div_big <= C % rectangle with long side <= C and short side <= R
for j = 1:R-div_small+1 % cycle over all possible rows
for k = 1:C-div_big+1 % cycle over all possible columns
no_of_ones=length(find(P(j:j-1+div_small,k:k-1+div_big))); % no. of ones in the current submatrix
if no_of_ones >= L && no_of_ones <= sH-L % if the submatrix contains at least L ones AND L zeros
% row indexes columns indexes area position
sub(counter,:)=[j,j-1+div_small , k,k-1+div_big , div_small*div_big , counter]; % save the submatrix
counter=counter+1;
end
end
end
disp([' [', num2str(div_small), 'x', num2str(div_big), '] submatrices: ', num2str(size(sub,1))])
end
if div_small~=div_big % if the submatrix is a square, skip this part (otherwise there will be duplicates in sub)
if div_small <= C && div_big <= R % rectangle with long side <= R and short side <= C
for j = 1:C-div_small+1 % cycle over all possible columns
for k = 1:R-div_big+1 % cycle over all possible rows
no_of_ones=length(find(P(k:k-1+div_big,j:j-1+div_small)));
if no_of_ones >= L && no_of_ones <= sH-L
sub(counter,:)=[k,k-1+div_big,j,j-1+div_small , div_big*div_small, counter];
counter=counter+1;
end
end
end
disp([' [', num2str(div_big), 'x', num2str(div_small), '] submatrices: ', num2str(size(sub,1))])
end
end
end
end
fprintf('\ntime: %2.2fs\n\n',toc)
Here is a solution centered around 2D matrix convolution. The rough idea is to convolve P for each submatrix shape with a second matrix such that each element of the resulting matrix indicates how many ones are in the submatrix having its top left corner at said element. Like this you get all solutions for a single shape in one go, without having to loop over rows/columns, greatly speeding things up (it takes less than a second for a 200x200 matrix on my 8 years old laptop)
P= [0 1 1 1 0 0 0
1 1 1 1 0 1 1
0 0 1 0 0 1 0
0 1 1 0 1 1 1
0 0 0 0 0 0 1
0 0 0 0 0 0 1];
L=1; % a submatrix has to containg at least L ones and L zeros
H=5; % max area of a submatrix
submats = [];
for sH = H:-1:2*L
div_sH=divisors(sH); % find all divisors of sH
for i = 1:length(div_sH) % cycle over all couples of divisors
%number of rows of the current submatrix
nrows=div_sH(i);
% number of columns of the current submatrix
ncols=div_sH(end-i+1);
% perpare matrix to convolve P with
m = zeros(nrows*2-1,ncols*2-1);
m(1:nrows,1:ncols) = 1;
% get the number of ones in the top left corner each submatrix
submatsums = conv2(P,m,'same');
% set values where the submatrices go outside P invalid
validsums = zeros(size(P))-1;
validsums(1:(end-nrows+1),1:(end-ncols+1)) = submatsums(1:(end-nrows+1),1:(end-ncols+1));
% get the indexes where the number of ones and zeros is >= L
topLeftIdx = find(validsums >= L & validsums<=sH-L);
% save submatrixes in following format: [index, nrows, ncols]
% You can ofc use something different, but it seemed the simplest way to me
submats = [submats ; [topLeftIdx bsxfun(#times,[nrows ncols],ones(length(topLeftIdx),1))]];
end
end
First, I suggest that you combine finding the allowable sub-matrix sizes.
for smaller = 1:sqrt(H)
for larger = 2*L:H/smaller
# add smaller X larger and larger x smaller to your shapes list
Next, start with the smallest rectangles in the shapes. Note that any solution to a small rectangle can be extended in any direction, to the area limit of H, and the added elements will not invalidate the solution you found. This will identify many solutions without bothering to check the populations within.
Keep track of the solutions you've found. As you work your way toward larger rectangles, you can avoid checking anything already in your solutions set. If you keep that in a hash table, checking membership is O(1). All you'll need to check thereafter will be larger blocks of mostly-1 adjacent to mostly-0. This should speed up the processing somewhat.
Is that enough of a nudge to help?
I'm having difficulties converting image pixels to coordinates and making them appear in my MATLAB workspace. For example, I have the image with pixel values as below (it's a binary image of size 4x4):
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
After getting the pixels, I want to read each value and if they're not equal to zero (which means 1), I want to read the coordinates of that value and save them in to my MATLAB workspace. For example, this is the idea that I thought of:
[x,y] = size(image)
for i=1:x
for j=1:y
if (image(i,j)~=0)
....
However, I am stuck. Can anyone give any suggestion on how to read the coordinates of the non-zero values and save them to my workspace?
Specifically, my expected result in the workspace:
2 2
2 3
3 2
3 3
Doing it with loops is probably not the most efficient way to do what you ask. Instead, use find. find determines the locations in a vector or matrix that are non-zero. In your case, all you have to do is:
[row,col] = find(image);
row and col would contain the row and column locations of the non-zero elements in your binary image. Therefore, with your example:
b = [0 0 0 0;
0 1 1 0;
0 1 1 0;
0 0 0 0];
We get:
>> disp([row, col]);
2 2
3 2
2 3
3 3
However, you'll see that the locations are not in the order you expect. This is because the locations are displayed in column-major order, meaning that the columns are traversed first. In your example, you are displaying them in row-major order. If you'd like to maintain this order, you would sort the results by the row coordinate:
>> sortrows([row, col])
ans =
2 2
2 3
3 2
3 3
However, if you really really really really... I mean really... want to use for loops, what you would do is keep two separate arrays that are initially empty, then loop through each pixel and determine whether it's non-zero. If it is, then you would add the x and y locations to these two separate arrays.
As such, you would do this:
row = []; col = [];
[x,y] = size(image);
for i=1:x
for j=1:y
if (image(i,j)~=0)
row = [row; i]; %// Concatenate row and column location if non-zero
col = [col; j];
end
end
end
This should give you the same results as find.
you can use meshgrid() to collect those coordinates. The function generates two outputs, first being x coordinates, second being y coordinates. you'd go like this:
[xcoord ycoord] = meshgrid( 1:x_size, 1:y_size);
zeros_coordsx = xcoord( image == 0);
zeros_coordsy = ycoord( image == 0);
this is way faster that nested looping and keeps you within matlab's natural vector operation space... these two outputs are in sync,meaning that
image( zeros_coordsy(1), zeros_coordsx(1))
is one of the zeros on the image
I am having trouble figuring out the dividing for the Marie Sim. I do not want the -1, but it keeps showing the negative. I only want it up to 2. Any suggestions?
This is my code:
ORG 100
Input
Store x
Input
Store y
loop, load x
Subt y
Store x
Load x
Output
Skipcond 0
Jump loop
Load x
Halt
x, Dec 0
y, Dec 0
Output:
11
8
5
2
-1
I noticed some of the above responses created infinite loops if the program encountered a remainder when dividing. I resolved this and my program outputs 2 decimal values the result and the remainder respectively.
This is a very old post but hopefully, this will be of use to any newcomers to Assembly.
STORE x
INPUT
STORE y
loop, Load x
If, Subt y
Skipcond 000
Jump Else
Then, Jump Endif
Else, Store x
Load Counter
Add One
Store Counter
Jump loop
Endif, Load Counter
Output
Load x
Output
HALT
x, Dec 0
y, Dec 0
Counter, Dec 0
One, Dec 1
//The code below fixes your problem but creates an infinite loop whenever the division problem has a remainder.
ORG 100
INPUT
STORE x
INPUT
STORE y
loop, LOAD x
SUBT y
STORE x
SKIPCOND 000
OUTPUT
SKIPCOND 400
JUMP loop
HALT
x, Dec 0
y, Dec 0
Try this:
Input
Store x
Input
Store y
loop, load x
Subt y
Store x
Load x
Output
Skipcond 400
Jump loop
Load x
Halt
x, Dec 0
y, Dec 0
ORG 100
input
store var1
input
store var2
loop, load var1
Subt var2
Store var1
Store remain
load qout
Add one
Store qout
Load remain
Subt var2
Skipcond 000
Jump loop
load qout
output
load remain
output
halt
var1, dec 0
var2, dec 0
one, dec 1
remain, dec 0
qout, dec 0
I have several maps that I am working with. I want to extract the values (1, 0 and NA) from the maps and place them all into a summary matrix. Since I have so many maps, I think its best to do this as a for loop. This is the code I have so far and my maps and empty summary matrix are uploaded to my Dropbox here: DATASET here
setwd ('C:/Users/Israel/Dropbox/')
require (raster)
require(rgdal)
require (plyr)
#load in the emxpy matrix to be filled
range.summary<-read.csv('range_sizes.csv', header=T)
#load in maps and count pixels
G1.total<-raster('Group1/Summary/PA_current_G1.tif')
G1.total.df<-as.data.frame(G1.total)
#these are the values I need to be placed into the empty matrix (range.summary)
count (G1.total.df)
PA_current_G1 freq
1 0 227193
2 1 136871
3 NA 561188
Try this
I downloaded 3 images
library(raster)
wd <- 'D:\\Programacao\\R\\Stackoverflow\\raster'
allfiles <- list.files(file.path(wd), all.files = F)
# List of TIF files at dir.fun folder
tifs <- grep(".tif$", allfiles, ignore.case = TRUE, value = TRUE)
#stack rasterLayer
mystack <- stack(file.path(wd, tifs))
# calculate frequencies
freqs <- freq(mystack, useNA='ifany')
# rbind list to get a data.frame
freqsdf <- do.call(rbind.data.frame, freqs)
freqsdf
value count
PA_2050_26_G1.1 0 256157
PA_2050_26_G1.2 1 193942
PA_2050_26_G1.3 NA 475153
PA_2050_26_G2.1 0 350928
PA_2050_26_G2.2 1 99171
PA_2050_26_G2.3 NA 475153
PA_2050_26_sub_G1.1 0 112528
PA_2050_26_sub_G1.2 1 90800
PA_2050_26_sub_G1.3 NA 721924
str(freqsdf)
'data.frame': 9 obs. of 2 variables:
$ value: num 0 1 NA 0 1 NA 0 1 NA
$ count: num 256157 193942 475153 350928 99171 ...
Now it is a matter of work the output shape.
I need to traverse a rectangular grid in continuous manner. Here is an example of what I want, the number means sequence:
+ x
y 0 1 2
5 4 3
6 7 8
At each step I know the index in matrix. Is there any way to calculate the coordinates? The inverse mapping for [x + y * width] doesn't help, beacuse it creates "steps" or "jumps". Is there any solution?
Here is explanation for "steps" mentioned above:
+ x
y 0 1 2
3 4 5 //at this moment the X coordinate changes by 3, thus create step
6 7 8
y = index / width
if( y % 2 == 0 )
x = index % width
else
x = width - index % width - 1
I think that should do it. It's a single modification of the standard way of calculating with "steps" as you call them. You are only changing the way the calculation is done based upon the row.
so you need to first increase the "x" component and then decrease right - so that you get a kind of snake-behavior? You will need an if statement (or some kind of modulo - magic). Let my try the magic:
y := floor(i/columnCount)
x = (y mod 2)*(i - y*columCount) + ((y+1) mod 2)*((columnCount -1) - (i - y*columnCount))