linked representation of sparse matrices - file

for sparse matrices i had put a question about its linked representation.
there were two lists to be implemented for a nine zero elements of a 5x8 matrix.....one is the coloumn list and another one is the row list.....
now here is a problem both the lists have properties as follows:
1) row list:- row,col,data,right
2) coloumn list:-row,col,data,down
the link is through either the down or right field
i have created a sparse matrice as follows:
_1_2_3_4_5_6_7_8_
1| 1 0 0 0 0 6 0 9
2| 0 2 0 0 0 0 7 0
3| 0 0 3 0 0 0 0 8
4| 0 0 0 4 0 0 0 0
5| 0 0 0 0 5 0 0 0
i googled around and finally got some representation in ibm site
but the real headache for me is i am just not getting enough help how to represent the elements as in both the list.
should both the list contain all 9 non zero elements?
can anybody suggest something?

the link list representation of the matrix is
|5|9|9| |-> |1|1|1| | -> |1|6|6| | -> |1|8|9| | -> |2|2|2| |->|2|7|7| |-> |3|3|3| |->|3|8|8| |->|4|4|4| |->|5|5|5|null|
here first node show the total number of row and columns.the 2nd ,3rd and so on shows the the values of row, column ,nonzero values of the corresponding values.
1| 1 0 0 0 0 6 0 9
2| 0 2 0 0 0 0 7 0
3| 0 0 3 0 0 0 0 8
4| 0 0 0 4 0 0 0 0
5| 0 0 0 0 5 0 0 0

Related

Consecutive elements of a vector in matlab

I am trying to display some value of n consecutive numbers of a vector (in this example, vector x).
x = [1
1
1
1
1
1
0
0
0
0
1
1
0
0
0
0
0
0
1
0
1
0
1
0
1
0
1
1
0
0
1
1
1
1
1
1
0
0
1
1
1
1
0
0
1
1
1
1
1
1
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
1
0
1
0
0
0
1
0
0
0
0
1
0
1
0
0
1
0
0
0
0
1
1
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
1
0
1
0
0
0
1
0
0
0
0
0
0
1
0
0
0
1
0
0
1
1
1
1
1
1
0
0
1
1
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0];
For example, I may want the first 3 values of 4 consecutive numbers which would be (3 values of 4 bits each):
1111
1100
0011
I may want the first 4 values of 2 consecutive numbers which would be (4 values of 2 bits each):
11
11
11
00
x is a double array. What would be an easy way to achieve this?
The simplest way is to reshape x to a matrix with your desired number of bits per value as the number of rows (MATLAB is column-major). For example, to get 4-digit values:
t = reshape(x,4,[]);
This will only work if the length of x evenly divides into 4. You could first crop x to the right number of elements to avoid errors where the length is not evenly divisible:
t = reshape(x(1:4*3),4,[]);
Now the transposed matrix t, converted to a string, looks like your desired output:
c = char(t.' + '0');
The output is a 3x4 char array:
'1111'
'1100'
'0011'
You can convert these binary representations back to numbers with bin2dec:
b = bin2dec(c);
The output is a 3-element double vector:
15
12
3

Converting observational data stored in a 2D data frame to a 3D array in R

I am trying to produce a 3D array from a 2D data frame in R and could really use some help. So far, I haven't found a solution to this problem from similar questions that have been posted previously.
My input data are available here: https://www.dropbox.com/s/7f8td34mpzgpvgh/example_data.csv?dl=0, and they resemble the following basic structure: I have 14 sites (i.e. Field) with 6 replicates each (i.e. Replicate) in which a subset of 32 subjects (i.e. columns of species codes: AMGO, BASW, etc.) were counted when present in a survey.
A subset of the input data looks like so:
example_data[1:5, 1:5]
Field Replicate AMGO BASW BHCO
1 Brinkman 1 2 0 0
72 Brinkman 2 10 0 0
190 Brinkman 3 6 0 0
283 Brinkman 4 0 0 0
342 Brinkman 5 2 1 0
I'd like to reformat these input data to resemble a 3D array (i.e. 14 sites x 6 replicates x 32 subjects), as exemplified below with the AMGO species:
, , = AMGO
1 2 3 4 5 6
Brinkman 0 0 0 0 0 0
Clara 0 0 0 0 0 0
Esckelson 0 0 0 0 0 0
GarnerEast 0 0 0 0 0 0
GarnerWest 0 0 0 0 0 0
KHess 0 0 0 0 0 0
Lounsbury 0 0 0 0 0 0
McCallum 0 0 0 0 0 0
Pomeroy 0 0 0 0 0 0
Sattelberg 0 0 0 0 0 0
THess 0 0 0 0 0 0
Turner 0 0 0 0 0 0
VollmarEast 0 0 0 0 0 0
VollmarWest 0 0 0 0 0 0
...
Note that, in the solution, many of the zeroes above would likely be replaced by non-zero counts when AMGO (and other species) was (were) actually encountered during a survey.
Please let me know if there's anything I need to clarify, and thanks in advance!
Here's a solution using the reshape() function from base R. I'm applying the function to each of the subject columns and creating a list of reshaped dataframes.
df <- read.csv("C:\\Users\\Shrivatav\\Downloads\\example_data.csv", encoding = "UTF-8")
# Extract subject columns
list.of.cols <- colnames(df)[3:34]
# Function for reshaping
func.for.reshaping <- function(column){
# Subset the data, keep only Field, replicate and the column input in the
# function
to.keep <- c("Field", "Replicate", column)
subset.df <- df[to.keep]
# reshape from long to wide
reshaped.df <- reshape(subset.df, idvar = "Field", timevar = "Replicate", direction = "wide")
return(reshaped.df)
}
# Apply the function over all subject columns, reulting
# in a list of dataframes
list.of.reshaped.dfs <- lapply(list.of.cols, func.for.reshaping)
# Name the list for easy access
names(list.of.reshaped.dfs) <- list.of.cols
You can access the elements of the list like: list.of.reshaped.dfs$AMGO and so forth.

Matlab finding the center of cluster of a few pixels and counting the clusters

So I have this matrix A, which is made of 1 and zeros, I have about 10 to 14 white spots of many pixels, but I want only 1 white pixel/centers coordinate for every cluster of white, how do I calculate how many cluster there are and their centers.
Try to imagine the matrix A as the night sky with white starts in black sky and how to I count the stars and the stars centers, plus the star are made of cluster of white pixels.
also the clusters are not all exactly the same size.
Here is some code using bwlabel and/or regioprops, which are used to identify connected components in a matrix and a buch of other properties, respectively. I think it suits your problem quite well; however you might want to adapt my code a bit as its more of a starting point.
clear
clc
%// Create dummy matrix.
BW = logical ([ 1 1 1 0 1 1 1 0
1 1 1 0 1 1 1 0
1 1 1 0 1 1 1 0
0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0
1 1 1 1 0 1 1 0
1 1 1 1 0 1 1 0
1 1 1 1 0 0 0 0]);
%// Identify clusters.
L = bwlabel(BW,4)
Matrix L looks like this:
L =
1 1 1 0 3 3 3 0
1 1 1 0 3 3 3 0
1 1 1 0 3 3 3 0
0 0 0 0 0 0 0 0
0 0 0 0 0 4 4 0
2 2 2 2 0 4 4 0
2 2 2 2 0 4 4 0
2 2 2 2 0 0 0 0
Here you have many ways to locate the center of the clusters. The first one uses the output of bwlabel to find each cluster and calculate the coordinates in a loop. It works and its didactic but it's a bit long and not so efficient. The 2nd method, as mentioned by #nkjt, uses regionprops which does exactly what you want using the 'Centroid' property. So here are the 2 methods:
Method 1: a bit complicated
So bwlabel identified 4 clusters, which makes sense. Now we need to identify the center of each of those clusters. My method could probably be simplified; but I'm a bit out of time so fell free to modify it as you see fit.
%// Get number of clusters
NumClusters = numel(unique(L)) -1;
Centers = zeros(NumClusters,2);
CenterLinIdices = zeros(NumClusters,1);
for k = 1:NumClusters
%// Find indices for elements forming each cluster.
[r, c] = find(L==k);
%// Sort the elements to know hot many rows and columns the cluster is spanning.
[~,y] = sort(r);
c = c(y);
r = r(y);
NumRow = numel(unique(r));
NumCol = numel(unique(c));
%// Calculate the approximate center of the cluster.
CenterCoord = [r(1)+floor(NumRow/2) c(1)+floor(NumCol/2)];
%// Actually this array is not used here but you might want to keep it for future reference.
Centers(k,:) = [CenterCoord(1) CenterCoord(2)];
%// Convert the subscripts indices to linear indices for easy reference.
CenterLinIdices(k) = sub2ind(size(BW),CenterCoord(1),CenterCoord(2));
end
%// Create output matrix full of 0s, except at the center of the clusters.
BW2 = false(size(BW));
BW2(CenterLinIdices) = 1
BW2 =
0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
Method 2 Using regionprops and the 'Centroid' property.
Once you have matrix L, apply regionprops and concatenate the output to get an array containing the coordinates directly. Much simpler!
%// Create dummy matrix.
BW = logical ([ 1 1 1 0 1 1 1 0
1 1 1 0 1 1 1 0
1 1 1 0 1 1 1 0
0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0
1 1 1 1 0 1 1 0
1 1 1 1 0 1 1 0
1 1 1 1 0 0 0 0]);
%// Identify clusters.
L = bwlabel(BW,4)
s = regionprops(L,'Centroid');
CentroidCoord = vertcat(s.Centroid)
which gives this:
CentroidCoord =
2.0000 2.0000
2.5000 7.0000
6.0000 2.0000
6.5000 6.0000
Which is much simpler and gives the same output once you use floor.
Hope that helps!

Algorithm for 'Pogo Painter' minigame

I am working on a minigame called 'Pogo Painter', and I need some mathematical solutions. Below is an image (made with Paint) to illustrate a bit what it's all about.
Four players, each of different color, must claim squares to gain points. The minigame will be similar to this: http://www.youtube.com/watch?v=rKCQfAlaRrc, but slightly different. The players will be allowed to run around the playground and claim any of the squares, and points are gathered when a pattern is closed. For example, claiming blue square on A3 will create a closed blue pattern.
What kind of variables should I declare and how do I check if the pattern is closed?
Please answer if you have a solution :)
Here’s another (Discrete Optimization) way to model your problem.
Notation
View your grid as a ‘graph’ with n^2 nodes, and edges of length 1 (Edges connect two neighboring nodes.) Let the nodes be numbered 1:n^2. (For ease of notation, you can use a double array (x,y) to denote each node if you prefer.)
Decision Variables
There are k colors, one for each player (1 through 4). 0 is an unclaimed cell (white)
X_ik = 1 if player k has claimed node i. 0 otherwise.
To start out
X_i0 = 1 for all nodes i.
All nodes start out as white (0).
Neighboring sets: Two nodes i and j are ‘neighbors’ if they are adjacent to each other. (Any given node i can have at most 4 neighbors: Up down right and left.)
Edge variables:
We can now define a new set of edge variables Y_ijk that connect two adjacent nodes (i and j) with a common color k.
Y_ijk = 1 if neighboring nodes i and j are both of color k. 0 Otherwise.
(That is, X_ik = X_jk) for non-zero k.
We now have an undirected graph. Checking for ‘closed patterns’ is the same as detecting cycles.
Detecting Cycles:
A simple DFS search will do, since we have undirected cycles. Start with each colored node i, and check for cycles. If a path leads you back to a visited node, cycles exist. You can award points accordingly.
Finally, one suggestion as you design the game. You can reward points according to the “longest cycle” you detect. The shortest cycle gets 4 points, one point for each edge (or one point for each node in the cycle) whichever works best for you.
1 1
1 1 scores 4 points
1 1 1
1 1 1 scores 6 points
1 1 1
1 1 1
1 1 scores 8 points
Hope that helps.
Okay,
This is plenty of text, but it's simple.
An N-by-N square will satisfy as the game-board.
Each time a player claims a square,
If the square is not attached to any square of that player, then you must give that square a unique ID.
If the square is attached,
Count how many neighbours of each ID it has.
( See the demos I put below, to see what this means)
For each group
patterns_count += group_size - 1
If the number of groups is more than 1
Change the ID of that group as well as every other square connected to it so they all share the same ID
You must remember which IDs belong to which players.
This is what you have in your example
1 1 1 0 0 0 0 2 2
1 0 0 0 1 3 3 0 0
1 1 0 0 3 3 0 0 0
0 1 0 0 4 5 0 0 0
0 0 0 6 4 0 0 0 0
7 7 0 0 0 0 8 8 8
0 7 7 0 9 8 8 0 8
A A 7 0 9 8 0 0 8
A 0 7 0 0 0 8 8 8
And this is what it would turn out like after blue grabs A-3
1 1 1 0 0 0 0 2 2
1 0 0 0 1 3 3 0 0
1 1 0 0 3 3 0 0 0
0 1 0 0 4 5 0 0 0
0 0 0 6 4 0 0 0 0
7 7 0 0 0 0 8 8 8
0 7 7 0 9 8 8 0 8
A A 7 0 9 8 0 0 8
A 0 7 0 0 8 8 8 8
More examples of the algorithm in use
1 1 1 0
1 0 1 0
1 1 0
0 0 0 0
2 neighbours. 2x'1'
1x closed pattern.
1 1 1 0
1 0 1 0
1 1 1 0
0 0 0 0
--
1 1 1 0 0
1 0 1 0 0
1 1 0 0
1 0 1 0 0
1 1 1 0 0
3 neighbours: 3x'1'
2x closed patterns
1 1 1 0 0
1 0 1 0 0
1 1 1 0 0
1 0 1 0 0
1 1 1 0 0
--
1 1 1 0 0
1 0 1 0 0
1 1 2 2
0 0 2 0 2
0 0 2 2 2
4 neighbours: 2x'1', 2x'2'
2 Closed patterns
1 1 1 0 0
1 0 1 0 0
1 1 1 1 1
0 0 1 0 1
0 0 1 1 1
But I also consider these a closed pattern. You haven't given any description as to what should be considered one and what shouldn't be.
1 1 0
1 1 0
0 0 0
1 1 1
1 1 1
0 0 0
1 1 1
1 1 1
1 1

rotate vector of arbitrary length circularly about an array about some point x,y in matlab

I have an array:
1 1 1 0 0
1 2 2 0 0
1 2 3 0 0
0 0 0 0 0
0 0 0 0 0
I want to make it
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
It is like rotating 1/4 piece of pie 270 degrees to fill out the remaining parts of the pie to make a full circle. Essentially mirroring the entire corner in all directions. I don't want to use any in built matlab features if possible - just some vector tricks if possible. Thanks.
EDIT:
This is embedded within an matrix of zeros of arbitrary size. I want it to work in both the above example and say this example:
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 1 2 2 0 0 0 0 0 0 0 0 0
0 0 1 2 3 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
Ideally, I want to have a vector say [1,2,3.. N] which can be rotated circularly about the highest value in the array (N) centered about some point xc,yc in the grid. Or if this isn't possible, take an base array [1 1 1, 1 2 2, 1 2 3] and rotate it such that 3 is in the centre and you fill a circle as in the 2nd matrix above.
EDIT:
I found rot90(M,k) rotates matrix M k times but this produces:
Mrot = M + rot90(M,1) + rot90(M,2) + rot90(M,3)
Mrot =
1 1 2 1 1
1 2 4 2 1
2 4 12 4 2
1 2 4 2 1
1 1 2 1 1
This stacks it in the x,y directions which isn't correct.
Assuming the corner you want to replicate is symmetric about the diagonal (as in your example), then you can do this in one indexing step. Given a matrix M containing your sample 5-by-5 matrix, here's how to do it:
>> index = [1 2 3 2 1];
>> M = M(index, index)
M =
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

Resources