How to create array of given length and same number? - arrays

When using SUMSERIES I need to specify "the array or range containing the coefficients of the power series" but I want to make it so the number of elements is dynamic while the element itself (1) remains the same.
Example:
SUM FROM 0 TO N of x^1,5
(cell) Length of series N : 7 -- > SUMSERIES(1,5;0;1;{1,1,1,1,1,1,1})
But I should be able to change the seven for a 3 and get --> SUMSERIES(1,5;0;1;{1,1,1})
In Java for example you'd declare and instantiate the array --> int[] arr = new int[N];
And then fill in a loop --> for(int i = 0; i <arr.length; i++) {arr[i] = 1,5}
Thanks in advance and sorry if the explanation isnĀ“t clear, it's my first time around hehe

this should work:
=SUMSERIES(1,5;0;1;SEQUENCE(1,[cell],1,0))

try:
=ARRAYFORMULA(SIGN(TRANSPOSE(ROW(INDIRECT("A1:A"&A1)))))
and then:
=INDEX(SUMSERIES(1,5; 0; 1; SIGN(TRANSPOSE(ROW(INDIRECT("A1:A"&A1))))))

In older version of Excel you can get this array using this (all of them are array formulas)
=INDEX(MUNIT(n),1,0)*0+x for horizontal array
=INDEX(MUNIT(n),0,1)*0+x for vertical array
Where:
n is dimension of the array
x is value of each item in the array
How it works:
MUNIT creates an identity matrix of size N
+---++---+---+---+---+---+
| || 1 | 2 | . | . | n |
+---++---+---+---+---+---+
+---++---+---+---+---+---+
| 1 || 1 | 0 | 0 | 0 | 0 |
| 2 || 0 | 1 | 0 | 0 | 0 |
| . || 0 | 0 | 1 | 0 | 0 |
| . || 0 | 0 | 0 | 1 | 0 |
| n || 0 | 0 | 0 | 0 | 1 |
+---++---+---+---+---+---+
Now we extract one (the first) row/column (n is set to 7 here)
=INDEX(MUNIT(7),1,0) for row extraction
=INDEX(MUNIT(7),0,1) for column extraction
And fill it with desired number (desired number here is 9 here)
=INDEX(MUNIT(7),1,0)*0+9 for row
=INDEX(MUNIT(7),0,1)*0+9 for column

Related

PostgreSQL: find minimum value across multiple columns but return column name

For each row in a table, I want to find the minimum value across a couple of numeric columns, then take the name of that column (which holds the desired value) and populate a new column with the name (or custom string).
A few rules first in my specific scenario: the value to be found across the columns must also be > 0. Also, if no value in the column is > 0, then a custom string should be placed (ie. 'none').
For example, take this table below with columns alpha to delta storing the values:
id | alpha | bravo | charlie | delta
------+--------+--------+---------+--------
1 | 5 | 2.3 | -1 | -5
2 | 9 | 8 | 3 | 1
3 | -1 | -4 | -7 | -9
4 | 6.1 | 4 | 3.9 | 0
for each row, I want to find out which column holds the lowest positive value. My expected output is something like this:
id | alpha | bravo | charlie | delta | lowest_postive
------+--------+--------+---------+--------+---------------
1 | 5 | 2.3 | -1 | -5 | 'col: bravo'
2 | 9 | 8 | 3 | 1 | 'col: delta'
3 | -1 | -4 | -7 | -9 | 'col: none'
4 | 6.1 | 4 | 3.9 | 0 | 'col: charlie'
Should I use a CASE ... WHEN ... THEN ...? Should I be converting the row into an array first, then assinging each position in the array?
You can do:
select *,
case when mp = alpha then 'col: alpha'
when mp = bravo then 'col: bravo'
when mp = charlie then 'col: charlie'
when mp = delta then 'col: delta'
end as lower_positive
from (
select *,
least(
case when alpha > 0 then alpha end,
case when bravo > 0 then bravo end,
case when charlie > 0 then charlie end,
case when delta > 0 then delta end
) as mp
from t
) x
However, this solution doesn't account for multiple minimums; the first one (from left ro right) wins.

How to generate long (up to 25 millions) random sequence of integers in C (with no repetition)?

I need to generate long (pseudo)random arrays (1000-25 000 000 integers) where no element is repeated. How do I do it since rand() function does not generate numbers long enough?
I tried to use this idea: array[i] = (rand() << 14) | rand() % length; however I suppose there is much better way that I don't know.
Thank you for your help.
You can use the Fisher-Yates shuffle for this.
Create an array of n elements and populate each element sequentially.
-------------------------
| 1 | 2 | 3 | 4 | 5 | 6 |
-------------------------
In this example n is 6. Now select a random index from 0 to n-1 (i.e. rand() % n) and swap the number at that index with the number at the top of the array. Let's say the random index is 2. So we swap the value at index 2 (3) and the one at n-1 (6). Now we have:
v
-------------------------
| 1 | 2 | 6 | 4 | 5 | 3 |
-------------------------
Now we do the same, this time with the upper bound of the index being n-2. Then we swap the value at that index with the value at index n-2. Let's say time we randomly get 0. So we swap index 0 (1) with index n-2 (5):
v
-------------------------
| 5 | 2 | 6 | 4 | 1 | 3 |
-------------------------
Then repeat. Let's say the next random index is 3. This happens to be our upper limit, so no change:
v
-------------------------
| 5 | 2 | 6 | 4 | 1 | 3 |
-------------------------
Next we get 0:
v
-------------------------
| 6 | 2 | 5 | 4 | 1 | 3 |
-------------------------
And finally 1:
v
-------------------------
| 6 | 2 | 5 | 4 | 1 | 3 |
-------------------------

use ((c1^c2) & ~32) to test if c1 and c2 are the same character in different case

I saw some code like this
if( ((c1^c2) & ~32)==0 )
{
...
}
In this snippet the code likely mean that if the if statement is true, then c1 and c2 are the same character in different case, meaning that one of those is +32 or -32 away from the other. Why is that?
I did test myself and discover that in some case it is true while in others not:
printf("%d", (65^97)& ~32); //output is 0. right
printf("%d", (97^65)& ~32); //output is 0. right
printf("%d", (50^82)& ~32); //output is 64!! not the same though 82-50=32
Why is that? what is the magic in it?
(c1^c2) & ~32) xors c1 and c2, the result contains the bits that are in both characters and & with ~32 clears (ignores) the bit 5. (It is zeroed whether it was same in both or not). Comparing this with zero, checks if all the bits other than bit 5 are same.
This can be used to check if 2 letters are equal ignoring their case in ascii representation if you are sure that atleast c1 or c2 is a valid latin character(a-z, A-Z).
To understand this, let's pick 2 characters with different case and compare them:
+---+---+---+---+---+---+---+---+
a | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
+---+---+---+---+---+---+---+---+
| x | | | | | |
+---+---+---+---+---+---+---+---+
A | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
a ^ A | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
32 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
~32 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
+---+---+---+---+---+---+---+---+
+---+---+---+---+---+---+---+---+
& | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+---+
You can try the same with j v/s J or t v/s z.
So there is no magic involved, only this logic.
Sometimes this condition is also written as:
if (ch1 == ch2 || (ch1 ^ 32) == ch2)
{
...
}

All the possible combinations from the decomposition of the elements from an array

For example, if i have this array: {2,3,1} the first element can be decompose in {0,1,2}, the second one in {0,1,2,3} and the third one in {0,1}. So i have to make the sum of every single combination what is possible to be make.
example:
0,1,0
0,0,0
0,3,1
2,3,1
.....
My idea to above this problem is make a kind of tree like this.
0 1 2
| | | | | | | | | | | |
0 1 2 3 0 1 2 3 0 1 2 3
| | | | | | | | | | | | | | | | | | | | | | | |
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
so my idea is make a Recursive function to make the sum:
1) move from top to bottom and when i find the limit change the move.
2) move from left to right, and when i find the limit in the right i return.
3) go back and move to the right, and back to the first step.
This is my code:
void tree(int*A, int i,int n,int j,int mov){
if(mov){ ///move from up to bottom.
if(i==n){ ///n is the large of the array.
return; ///
tree(A,i-1,n,j+1,0);
}
tree(A,i+1,n,j,1);
}
else{ ///move from left to right.
if(j>A[i]){ ///j is decomposition of the element.
return;
}
tree(A,i,n,j+1,0);
}
}
I don't make any sum, I'm just trying to go over all the possible combination. But it's not working. I think is a problem whit what is being returning when the function find a limit.
I really want to solve it by myself but I'm in a dead point right now, maybe a need to know something else about recursive functions, so please don't put a complete solution, i want to learn by my way. I just want tips about what can i do. Is this the best way to solve this problem?
Also, please tell if the problem it's clear, my English is basic so i have problems to express what i want.

Visualize incomplete 2D array

How can i visualize 2D array with surface(mesh, surf) for incomplete dataset?
'Incomplete' means (v - known values, 0 - unknown):
1 | 2 | 3 | 4 | 5
1 | v | 0 | v | 0 | v
2 | 0 | 0 | 0 | 0 | 0
3 | v | 0 | v | 0 | v
4 | v | 0 | v | 0 | v
5 | 0 | 0 | 0 | 0 | 0
Such data indexing is handy for analyzing non-linear relation between variables.
The thing i want is working somehow with plot function. Lets say, x = [1,2,4,5]. plot will show continuous figure.
Is it possible to do so for 2D arrays without manual interpolation? Don't care about smoothness. Linear connection of known points is alright.
So you have non-linear sampling (x = [1 3 5], y = [1 3 4]), and you don't want to interpolate? I don't think surf etc will handle it. Sounds like a job for plot3.
This is mildly ugly (see result) but I'm presuming you just want to visualise it to get a feel for the data. First make up your x and y with repmat if you don't already have them like this:
x =
1 3 5
1 3 5
1 3 5
y =
1 1 1
3 3 3
4 4 4
Then you'll need your values without all the zeros in to match:
z =
6 8 10
6 5 4
4 2 1
This can be plotted with markers (might be the simplest if you have lots of points). Or you can use this trick to make a "mesh" out of two sets of lines:
plot3(x,y,z)
hold on
plot3(x',y',z')
xlabel('x');
ylabel('y');
Exactly as with your plot example, this simply linearly connects between the existing points.
You could replace the 0 values for NaN values.
Both, surf and mesh work with NaN.

Resources