A B C D
E F G H
I J K L
M N O P
If I chose to join the columns I would ={A1:A;B1:B;C1:C;D1:D} but it would look like this:
A
E
I
M
B
F
J
N
... and so on
I would like it to look like this:
A
B
C
D
E
F
G
... and so on
How to proceed in this case?
Note: It may happen that some of the columns are not complete in data, some may have more values than the others, but I still want to continue following this same pattern. Example:
A B D
E G H
I J K L
M N O P
Result:
A
B
D
E
G
H
... and so on
use:
=TRANSPOSE(QUERY(TRANSPOSE(A:D),, 9^9))
then:
=TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(A:D),,9^9)),,9^9), " "))
This kind of question pops up on SO a lot, but bear with me, it has a twist.
Let's say you have the following array...
[A, B, C, D, E, F, G, H , I, J, K, L]
Which you map to a grid like this
| 0 1 2 3
----------
0| A D G J
1| B E H K
2| C F I L
If you need to find the original array index for any item in the grid, you can use this formula (rows and cols are zero based).
col * numRows + row
So the index for 'I', which sits at coords 2,2 on the grid would be...
2 * 3 + 2 = 8
Simples! So now imagine you have two arrays...
[A, B, C, D, E, F, G, H , I, J, K, L]
[#, &, *, #, €, %, ƒ, œ, ≈, ∑, ß, Ω ]
Which is laid out in a grid like this...
0 1 2 3 4 5 6 7
-----------------
0| A # D # G ƒ J ∑
1| B & E € H œ K ß
2| C * F % I ≈ L Ω
Now you need to constrain the column indexes so that 0 & 1 = 0, 2 & 3 = 1 etc etc. For that, I've come up with this formula.
Math.floor(column / numArrays) * numRows + row;
Now let's turn it up a gear. Imagine you have these two arrays (16 items in each)
[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8]
[100,100,200,200,300,300,400,400,500,500,600,600,700,700,800,800]
But now, you want the grid to display rows where pairs of values have been summed
0 1 2 3
----------------
0| 2 200 10 1000
1| 4 400 12 1200
2| 6 600 14 1400
3| 8 800 16 1600
And you want to find the first index in the original array, e.g. the index for the value 8, would be 15, or the index for value 400 would be 2.
I'd made the naive assumption that you could just multiply the row and column indexes by the chunk size I'm was using to sum the values. That is however, completely wrong.
So, is it possible to derive the index in a flat array using row and column indexes when the grid is composed of multiple arrays whose values have been interleaved, AND summed (or as I call it, 'collapsed')?
UPDATE: I've made a little bit of progress. If my second formula was the function
func coordsToIndex(col, row, totalDataSets, totalRows) {
return Math.floor(col / totalDataSets) * totalRows + row;
}
And if the variable chunkSize indicates how many values are being summed together, then this
coordsToIndex(col, row, totalDataSets, totalRows) * chunkSize
Comes close, but the resulting index is always too large by the chunk size. SO!
(coordsToIndex(col, row, totalDataSets, totalRows) * chunkSize) - chunkSize
Gives me the correct value, BUT only when the chunkSize is greater than zero, which leads me to...
index = (coordsToIndex(col, row, totalDataSets, totalRows) * chunkSize)
realIndex = chunkSize > 1
? (index * chunkSize) - chunkSize
: calculatedIndex;
I sense that there's a neater way to do this and that I'm missing something glaringly obvious.
I am trying to generate a password with certain requirements.
When I enter the while loop to generate a random character from the array it is fine until I add a count for my index "$i"
With the following code:
#!/bin/bash
#SET ARRAY VALUES
all=( 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z )
echo
#SET COUNT VALUES TO 0
numc=0
lowc=0
upc=0
i=0
while true;
do
#GENERATE PASSWORD
phrase[$i]=${all[$RANDOM%62]}
#CHECK IF PASSWORD MEETS REQUIREMENTS
for ((n=0; n<10; n++))
do
if [ ${phrase[$i]} == ${all[$n]} ]
then
echo num ${all[$n]}
let "numc++"
let "i++"
fi
done
I get the error "line 39: [: ==: unary operator expected"
but if I remove the let "i++" line then there is no error. But I need to increase my index in order to exit the loop and check the minimum length of the password
If ${phrase[$i]} (by the way you don't need $ in [$i] context ${phrase[i]} works too) is ever the empty string that if test will become [ == value-of-all-n ] which isn't a valid test.
Either quote the variables (which is almost always the right thing to do) or prevent that from ever being the empty string. (Was that i++ supposed to happen outside the inner loop?)
Hi I want to reshape a matrix but the reshape command doesn't order the elements the way I want it.
I have matrix with elements:
A B
C D
E F
G H
I K
L M
and want to reshape it to:
A B E F I K
C D G H L M
So I know how many rows I want to have (in this case 2) and all "groups" of 2 rows should get appended horizontally. Can this be done without a for loop?
You can do it with two reshape and one permute. Let n denote the number of rows per group:
y = reshape(permute(reshape(x.',size(x,2),n,[]),[2 1 3]),n,[]);
Example with 3 columns, n=2:
>> x = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
x =
1 2 3
4 5 6
7 8 9
10 11 12
>> y = reshape(permute(reshape(x.',size(x,2),n,[]),[2 1 3]),n,[])
y =
1 2 3 7 8 9
4 5 6 10 11 12
Cell array approach -
mat1 = rand(6,2) %// Input matrix
nrows = 3; %// Number of rows in the output
[m,n] = size(mat1);
%// Create a cell array each cell of which is a (nrows x n) block from the input
cell_array1 = mat2cell(mat1,nrows.*ones(1,m/nrows),n);
%// Horizontally concatenate the double arrays obtained from each cell
out = horzcat(cell_array1{:})
Output on code run -
mat1 =
0.5133 0.2916
0.6188 0.6829
0.5651 0.2413
0.2083 0.7860
0.8576 0.3032
0.1489 0.4494
out =
0.5133 0.2916 0.5651 0.2413 0.8576 0.3032
0.6188 0.6829 0.2083 0.7860 0.1489 0.4494
I'm working on a C program that crops .ppm files from a starting point pixel (x,y) (top left corner of cropped image) to an end point pixel (x+w,x+h)(bottom left corner of cropped image).
The data in .ppm files is of the following format:
r g b r g b r g b r g b r g b r g b
r g b r g b r g b r g b r g b r g b
r g b r g b r g b r g b r g b r g b
r g b r g b r g b r g b r g b r g b
Is there a simple way, wich avoids the use of 2 dimensional arrays, to do this using scanf()?
One easy way would be to simply keep track of your pixel coordinate as you read the file in. If you're currently in the crop rectangle, store the pixel; otherwise, skip it.
If you want to get more fancy: figure out the byte offset for the start of each row, seek to it, then read in the whole row.
Warning, some pnm files are in binary mode (they differ by magic number in the beginning of the file contents).
Maybe lookup the sources of pnmcrop would help?