Convert a double array into binary using series - matlab

I have a series as [ 7 5 4 3 2 1] . I want to convert a number into binary sequence by considering the place value of the above numbers. So each number have many combinations like..
3 is represented as [ 0 0 0 1 0 0 ] again 3 is represented as [ 0 0 0 0 1 1]
4 is represented as [ 0 0 1 0 0 0 ] again 4 is represented as [ 0 0 0 1 0 1]
5 is represented as [ 0 1 0 0 0 0 ] again 5 is represented as [ 0 0 0 1 1 0]
5 is represented as [ 0 0 1 0 0 1]
I want to store all values in an array. Can anybody help me for writing Matlab Code for it?

Related

Adding all the 1's in a row?

I have a very large matrix(around 4000000x2) , and it has 1s sprinkled throughout the matrix. What I want to do is that I just want to add up all the 1s in one row.
For example, if I have a matrix like this:
A = [0 0 4 1 0 0 1
1 0 5 0 7 0 1
5 6 0 8 1 0 6
0 9 5 1 0 0 0]
Is there a way of summing up all of the 1's a row? For example, here it would be:
sum = [2
2
1
1]
I know that if you want to add up the whole row, you can use sum(A,2). But is there a way in matlab to add up all of a specific number? I'm new to matlab and I would greatly appreciate any help, thank you!!
Generate an array that has a 1 everywhere A has a 1, and 0 everywhere else:
>> A == 1
ans =
0 0 0 1 0 0 1
1 0 0 0 0 0 1
0 0 0 0 1 0 0
0 0 0 1 0 0 0
Then you can just use sum:
sum(A == 1, 2)

Create coordinate array from adjacency matrix - Matlab

I have a quick graph theory question.
I have a 13 x 13 adjacency matrix in Matlab. I've already taken just the lower diagonal (this is an undirected graph) and subtracted off the identity matrix (so there aren't edges joining a node to itself). I then added a column on the left and a row on the top with the ID numbers of the four nodes. The resulting 14 x 14 adjacency matrix looks like this:
A =
0 1 1 1 1 2 2 2 3 3 3 4 4 4
1 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 1 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0 0
3 0 1 0 0 0 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 0 0 0 0 0 0
4 0 0 0 1 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 1 0 0 0 0 0 0
How can I create the coordinate array from this? I know the result should have 7 rows (one for each unique node pair).
Please let me know if you can help. Thanks in advance!
We can use the node IDs that are provided at the first row and first column to help us create a node adjacency list. What we need to do is split up the variables so that we separate out the first row, called rowList and the first column colList. We will denote these the row and column lists respectively. We will also extract the adjacency matrix which is the rest of the matrix. The basic algorithm is the following:
Find those rows and columns that are non-zero in the adjacency matrix.
Use these rows and columns to index the corresponding rows and columns lists and spit out a co-ordinate array.
Without further ado:
rowList = A(2:end,1);
colList = A(1,2:end).';
AdjMatr = A(2:end,2:end);
[nonZeroRows, nonZeroCols] = find(AdjMatr);
nodeList = [rowList(nonZeroRows) colList(nonZeroCols)];
The output thus gives:
nodeList =
2 1
4 1
3 1
3 1
4 1
4 2
4 2
This answer does not give unique rows of course, and produces duplicates. If you wish to have unique rows, consider doing:
nodeListUnique = unique(nodeList, 'rows');
The output is:
nodeListUnique =
2 1
3 1
4 1
4 2
It appears that what you want is:
[ii, jj] = find(A(2:end,2:end)); %// row and col indices of ones in inner matrix
result = [ A(ii+1,1) A(1,jj+1).' ]; %'// node numbers corresponding to ii and jj
In your example, this gives
result =
2 1
4 1
3 1
3 1
4 1
4 2
4 2
If you need unique rows:
result = unique(result, 'rows');
which gives
result =
2 1
3 1
4 1
4 2

Copying part of a matrix to another empty matrix with the same indices

I'm trying to copy part of a matrix (matrix 1) in matlab to another empty matrix of zeros (matrix 2) so that the section I copy from matrix 1 has the same indices in matrix 2, e.g.
Matrix 1 (mat1):
0 3 0 0 2 4 1 2 6
1 3 4 2 0 0 0 2 0
0 2 6 1 3 6 6 1 1
0 0 0 2 1 3 3 1 0
1 4 5 2 3 3 0 0 1
Matrix 2 (mat2) desired output:
0 0 0 0 0 0 0 0 0
0 0 4 2 0 0 0 0 0
0 0 6 1 3 6 6 0 0
0 0 0 2 1 3 3 0 0
0 0 0 0 0 0 0 0 0
I've tried something like
mat2([2:4],[3:7]) = mat1([2:4],[3:7])
but of course it doesn't work... any ideas of an efficient way to do this? I couldn't find another thread to help with this problem.
Thanks!
It does work. You just need to create mat2 first:
mat2 = zeros(size(mat1));
mat2(2:4, 3:7) = mat1(2:4, 3:7);
Note that you don't need the square brackets on those ranges.
Do this:
mat2 = zeros(size(mat1));
Before copying over.

every other matching element in matrix MATLAB

I have a matrix of 2 values, for example:
[ 1 1 1 1 1 0 0 0 1 1 0 0 0
0 0 0 0 1 1 1 1 0 0 0 1 1 ]
I want to to change every other element in a row that is a "1" to a 0,
so the output would be:
[ 0 1 0 1 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 1 0 0 0 0 1 0 ]
if your matrix is m then you can do:
ind=find(m(:)==1);
m(ind(1:2:end))=0;

Multiplication of Matrix , Translocation of 3D object in Perl

I want to multiply two matrices in Perl. 1st {n*4} with 2nd {4*n}. I want to fetch the values from a text file. my one text file looks like
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
and othe one is like the following
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
Please help me. My main motif is to multiply a 3D x,y,x coordinates with some value to translate it to other position. Thanks in advance.
Use Math::Matrix.
Multiplies two matrices where the length of the rows in the first matrix is the same as the length of the columns in the second matrix. Returns the product or undef in case of error.
PDL::Matrix can be used:
#!/usr/bin/env perl
use strict;
use warnings;
use PDL;
use PDL::Matrix;
if ( #ARGV != 2 ) {
die 'Two matrix files are required as arguments';
}
my $index = 0;
my #matrices;
while (<>) {
push #{ $matrices[$index] }, [ split /\s+/ ];
}
continue { $index++ if eof }
my $matrix_1 = PDL::Matrix->pdl( #{ $matrices[0] } );
my $matrix_2 = PDL::Matrix->pdl( #{ $matrices[1] } );
print $matrix_1 x $matrix_2;
The PDL module is suited for matrix computations.
Use PDL::Matrix and rcols
Use rcols to read in data files of whitespace separated columns. For example, using the current PDL-2.4.10 release via the pdl2 shell we demonstrate:
pdl> #cat an4.cols; # this is the [n,4] data file
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
pdl> #cat a4n.cols; # this is the [4,n] data file
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
pdl> $a = rcols 'a4n.cols', []; # read col data into 2-D piddle
Reading data into piddles of type: [ Double ]
Read in 40 elements.
pdl> $b = rcols 'an4.cols', []; # read col data into 2-D piddle
Reading data into piddles of type: [ Double ]
Read in 36 elements.
pdl> $am = PDL::Matrix->pdl($a); # covert to usual matrix dim order
pdl> $bm = PDL::Matrix->pdl($b); # covert to usual matrix dim order
pdl> p $cm = $am x $bm; # multiply the two matrices
[
[10 10 10 10 10 10 10 10 10]
[ 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 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 0 0 0 0 0 0 0]
]
pdl> help vars # note the dim order change between $a and $am, etc
PDL variables in package main::
Name Type Dimension Flow State Mem
----------------------------------------------------------------
$a Double D [4,10] VC 0.00KB
$am Double D [10,4] VC 0.00KB PDL::Matrix
$b Double D [9,4] VC 0.00KB
$bm Double D [4,9] VC 0.00KB PDL::Matrix
$cm Double D [10,9] P 0.70KB PDL::Matrix
$Pi Double D [] P 0.01KB
Note: the only difference between $am and $a are that for the PDL::Matrix objects the dimension order convention for the 2-D matrix is reversed from the standard PDL convention. For more information on all things PDL, please go to the PDL website where you'll find links to documentation, mailing list archives, and more...