matlab matrix from array - arrays

I have large array. Now I need a matrix with 8 elements in every row. My array looks like this:
A= Columns 1 through 18
0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 0 0
Columns 19 through 36
0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0
and so on. How I can get [nx8] matrix? For example:
B=[0 0 0 0 0 0 0 0
0 0 1 1 0 1 1 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0]
I've tried reshape, but it didn't work correctly. I get one 1 where shouldn't be.
B=reshape(A,[],8)

You almost have it. The problem is that Matlab fills the matrix column-wise, whereas you seem to want it filled row-rise. So create an 8-row matrix and then transpose:
reshape(A,8,[]).'

What about vec2mat
vec2mat
vec2mat(A,8)

Related

Why does summing the rows in this matrix give me 0?

I created an array A by first using the command A = [1:10]'.
Then I created a 10x10 matrix, only containing 0's. I then overwrote this matrix with my A, resulting in this new matrix:
A =
1 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 0 0
Now the problem is, when I run the command sum((A(1,1)):(A(1,end))), I keep getting 0 when I should be getting 1, as it is the sum of the first row. I tried running the same command on other matrices and they give me the correct answer, so why isn't it working here?
The term
(A(1,1)):(A(1,end))
creates and empty array, as A(1,1) = 1 and A(1,end) = 0 which makes it impossible for colon : to create a vector, so the sum over it is zero. But its not what you want anyway, I guess.
What you supposedly want is
sum(A(1,:))
or in respect to whole matrix, by specifying the dimension of the sum, e.g.
sum(A,2)
ans =
1
2
3
4
5
6
7
8
9
10
Edit
If you want to start from a different column index you can do the following:
sum( A(rowIndex,firstColumnIndex:lastColumnIndex) )
while end can be used as macro variable for the last index of the corresponding column or row.

The fastest way to set up sparse matrix in matlab

I am working with iterative methods, and thus with large sparse matrices.
For instance, I want to set up a matrix like this:
1 1 0 0 1 0 0 0 0 0
1 1 1 0 0 1 0 0 0 0
0 1 1 1 0 0 1 0 0 0
0 0 1 1 1 0 0 1 0 0
1 0 0 1 1 1 0 0 1 0
0 1 0 0 1 1 1 0 0 1
So that only certain diagonals are non-zero. In my programming, I will be working with much larger matrix sizes, but Idea is the same: Only a few diagonals are non-zero, all other entries are zeros.
I know, how to do it in for loop, but it seems to be not effective, if the matrix size is large. Also I work with symmetric matrices.
I would appreciate, if you provide me a code for my sample matrix along with description.
You want spdiags:
m = 6; %// number of rows
n = 10; %// number of columns
diags = [-4 -1 0 1 4]; %// diagonals to be filled
A = spdiags(ones(min(m,n), numel(diags)), diags, m, n);
This gives:
>> full(A)
ans =
1 1 0 0 1 0 0 0 0 0
1 1 1 0 0 1 0 0 0 0
0 1 1 1 0 0 1 0 0 0
0 0 1 1 1 0 0 1 0 0
1 0 0 1 1 1 0 0 1 0
0 1 0 0 1 1 1 0 0 1

Check all diags in square matrix for true

I am trying to check if in a square matrix there is more than one true value in all possible diagonals and anti-diagonals, and return true, otherwise false.
So far I have tried as following but is not covering all possible diagonals:
n=8; %matrix dimension 8 x 8
diag= sum(A(1:n+1:end));
d1=diag>=2;
antiDiag=sum(A(n:n-1:end));
d2=antiDiag>=2;
if ~any(d1(:)) || ~any(d2(:))
res= true;
else
res=false;
end
this is a false:
0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 1 0 0 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 1
this is a true:
0 0 0 0 0 0 0 1
0 0 0 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 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Since these are my first steps in using Matlab, is there a specific function or a better way to achieve the result I am looking for?
To detect if there are more than one nonzero value in any diagonal or anti-diagonal (not just the main diagonal and antidiagonal): get the row and column indices of nonzero values, ii and jj; and then check if any value of ii-jj (diagonals) or ii+jj (anti-diagonals) is repeated:
[ii, jj] = find(A);
res = (numel(unique(ii-jj)) < numel(ii)) || (numel(unique(ii+jj)) < numel(ii));
One approach:
n=8; %// size of square matrix
A = logical(randi(2,n)-1); %// Create a logical matrix of 0s and 1s
d1 = sum(A(1:n+1:end)); %// sum all the values of Main diagonal
d2 = sum(A(n:n-1:end-1)); %// sum all the values of Main anti-diag
result = d1>=2 | d2>=2 %// result is true when any one of them is > than or = to 2
Sample run:
Inputs:
>> A
A =
0 1 1 1 1 0 1 0
0 1 1 1 1 1 0 0
0 1 0 1 1 0 0 1
0 1 1 0 1 1 0 0
0 1 0 1 1 0 0 1
1 0 0 0 1 1 0 1
1 1 1 1 1 1 0 0
1 1 1 1 0 0 0 1
Output:
result =
1
Note: This approach considers only the Main diag and Main Anti-Diag (considering the example you provided). If you want for all possible diags, the other answer from Luis Mendo is the way to go
Using #Santhan Salai's generating technique, we can use the diag function (to pull out the main diagonal of the matrix), the fliplr to flip over the center column and any to reduced to a single value.
n=8; %// size of square matrix
A = logical(randi(2,n)-1); %// Create a logical matrix of 0s and 1s
any([diag(A) ; diag(fliplr(A))])

How does this Matlab/Octave code create a Boolean matrix from a vector?

The first line of code creates some vector with "discrete labels", and the second line of code creates a sparse matrix with ones at the index that the label represents. "eye" creates an identity matrix, but then even if the vector "a" is much longer, this effect of creating a sparse matrix still works!?
Could you please help me understand what is going on?
octave:4> a = [1 3 5 7 9 2 4 6 8 10]
a =
1 3 5 7 9 2 4 6 8 10
octave:5> eye(10)(a,:)
ans =
Permutation Matrix
1 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 1
The notation eye(10)(a,:) in Octave means: build the size-10 identity matrix (eye(10)) and then pick its rows in the order given by a (note that a is used as the first index, which corresponds to rows, and : as second index, which means "take all columns"). So, for example, the 4th row of the result is row 7 of the identity matrix, because the 4th entry of a contains 7.
From this explanation it's clear that a can be as long as you want, provided that all its values are integers in the range 1...10 (these are the rows available in eye(10)).
Note that in Matlab this "chained" indexing is not allowed. You would have to first assign eye(10) to a variable, and then index into that variable:
m = eye(10);
m(a,:)
Lastly, a minor "technical" note: the obtained matrix is not of type logical (Matlab's Boolean data type), nor is it sparse. Rather, it's a full matrix of type double.

Binary Matrix SQL

I want to store a binary matrix into a database.
Matrix example:
0 1 0 0 1 1 0 1
0 1 0 0 0 1 0 0
0 1 1 0 0 1 1 1
0 1 0 0 1 1 0 0
0 1 0 0 0 1 0 0
0 1 1 0 0 1 0 0
0 1 0 1 0 0 0 0
0 1 0 0 1 1 1 0
Which is the best way to do that?
OBS: I don't understand much of db.
Thanks.
you can store it as varchar(n^2). If you don't need to work on it in the DB, you can also store each line in the matrix as it's value- for example, your first line will be 77.
or if you know the number of digits in any line and colomn you can save it in one decimal(11135377233198751900) or hexadecimal(9A88CE9888C8A09C) number

Resources