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.
Related
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.
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
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 |
-------------------------
I've been trying to learn about minimum span trees and the algorithms associated with it, namely Prim's, Kruskal's and Dijkstra's algorithms.
I understand how these algorithms work and have seen them in action but there is only one thing I don't understand about Prim's algorithm, which is an array that I don't understand what it is it's intention and how does it work.
So here is the situation:
I have to do an exercise wherein I am given an adjacency table and I have to run Prim's algorithm to create a minimum span tree.
The table looks like this:
0 |1|2| 3| 4| 5|
0| 0 73 4 64 40 74
1| 73 0 46 26 30 70
2| 4 46 0 77 86 14
3| 64 26 77 0 20 85
4| 40 30 86 20 0 22
5| 74 70 14 85 22 0
The numbers separated by the "|" are the vertices and the numbers in the table are the edges. Simple, I run the algorithm ( in this website for example: http://www.jakebakermaths.org.uk/maths/primsalgorithmsolverv10.html ) or just jot it down on paper and and draw the minimum span tree and I get the tree with the minimal cost of 86 and the edges that have been used are 4, 26, 20, 22 and 14.
Now here comes the problem, apparently just solving it wasn't enough. I need to find the values of an array called closest[0,...,5]. I know it is used in the algorithm but I don't know it's purpose and what I should do with it or how to get it's values.
I have searched the internet for it and found this link about Prim's algorithm:
http://lcm.csa.iisc.ernet.in/dsa/node183.html
Which defines the array "closest" as "For i in V - U, closest[i] gives the vertex in U that is closest to i".
I still don't understand what it is, what it is used for and what the values inside of them are.
All I know the answer to my exercise is
closest[1] = 3
closest[2] = 0
closest[3] = 4
closest[4] = 5
closest[5] = 2
Thank you in advance.
When doing a MST with Prim's algorithm, it is important to keep track of four things: the vertex, has it been visited, minimal distance to vertex, and what precedes this vertex (this is what you are looking for).
You start at vertex 0, and you see that the closest vertex to 0 is 2. At the same time, you could have visited all other nodes, but with bigger distances. Nevertheless, the closest node to 0 is 2, so thus 2 becomes visited and its parent is set to vertex 0. All the other nodes are not visited yet, but its parent as of now is set to 0, with its respective distance. You now need to set the smallest distance vertex to visited, and now consider this node as the node to be considered.
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 73 | 0
2 | T | 4 | 0
3 | F | 64 | 0
4 | F | 40 | 0
5 | F | 74 | 0
We then check all the distances of nodes from 2. We compare the new distances from 2 to the other nodes to the distance from the other nodes from its previous distance, and if it needs to be updated, it gets updated. We now see that the distance from 2 to 5 is shorter than 0 to 5, and vertex 5 now becomes becomes visited, with its parent now equal to vertex 2.
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 46 | 2
2 | T | 4 | 0
3 | F | 64 | 0
4 | F | 40 | 0
5 | T | 14 | 2
Now we visit 5. One thing to note is that if a node is visited, we do not consider it in our distance calculations. I have simulated the rest, and hopefully you can see how you get the answer you're looking for.
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 46 | 2
2 | T | 4 | 0
3 | F | 64 | 0
4 | T | 22 | 5
5 | T | 14 | 2
Now visit 4
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 46 | 2
2 | T | 4 | 0
3 | T | 20 | 4
4 | T | 22 | 5
5 | T | 14 | 2
And now visit 3
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | T | 26 | 3
2 | T | 4 | 0
3 | T | 20 | 4
4 | T | 22 | 5
5 | T | 14 | 2
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.