Stack a n x n matrix to from a n^2 x 1 column matrix - matlab

for a 5 x 10 matrix, I wanted to stack the matrix in such a way that each column of the original matrix will be appended to the rows to finally end up with a 50 x 1 matrix. Basically stack the columns into 1 column. Thanks.
Here is the start of the matrix:
RR = randi(5,5,10);

For general reshaping operations, use reshape, for example:
reshape(RR,[],1)
This reshapes the array RR into an array with an indeterminate number of rows (the []) and 1 column, as you need.
Your particular case can use the shortcut mentioned by #beaker in the comments
RR(:)
This syntax is equivalent to the reshape command above, and simply lists, in one column, all elements of the array.
Both of these examples take advantage of the fact that Matlab uses column oriented storage and indexing. If, for some reason, you need to concatenate rows rather than columns, you would need to transpose the RR array first. For example:
reshape(RR', 1, []) %This concatenates the rows of RR into a single row. Note the transpose operator.

Related

MATLAB indexing for a matrix

If T is a 3x3 matrix.. what does T(1:2,3) = k mean?
I think T(1:2) means the first 2 columns. So does T(1:2,3) mean the first two columns in the last row..?
You are assigning the value k to the first two rows of column 3.

MATLAB matrix operation

I am having matrix with approx 3000 rows(changing) and 3 columns.
I have count of both rows and columns.
I am trying to plot the graph:
x=1:3000;
plot(matrix(x,1))
is there any way that I can include all rows in the plot instruction itself so that I can remove 'x=1:3000' ?
Also, I want to divide, 1st column of matrix which have 3000 rows into another matrix of 3 columns each with 1000 rows. Any specific instruction for this ?
I have made for loop for this and then i am placing individually the elements in the new array. But its taking long time.
As to the plotting issue, using the colon operator will plot all rows for your desired column:
plot(matrix(:,1));
EDIT: You mentioned you were a beginner. In case you haven't seen the colon operator used like this before, a colon operator all by itself when indexing into a matrix essentially means "all __", either "all rows" if in the first position or "all columns" if in the second position.
As for the second question, of splitting one column into a new matrix with multiple columns, you can use the reshape() function, which takes the input matrix to be reshaped and a number of output rows and columns. For example, to split the first column of matrix into 3 columns and put them into newMatrix, use the following:
newMatrix = reshape(matrix(:,1),[],3);
Note that the above code uses [] in the second argument (the number of rows argument) to mean "automatically determine number of rows".This is automatically determined based on the number of columns, which is defined in the third argument here as 3. The reshape function requires that the number of output rows * output columns be equal to input rows * input columns. So in the above case this will only work if the starting matrix has a number of rows which is divisible by 3.

Correlation and array in MATLAB

Kindly somebody help me in this.
I have two arrays of equal size 8x8.
And I need covariance of Column 1 of array 1 with the column 1 of second one.
After that I want to find Column 1 with the column 2 of second array.
After that I want to find Column 1 with the column 3 of second array.
After that I want to find Column 1 with the column 4 of second array.
After that I want to find Column 2 with the column 1 of second array.
And so on
I am assuming you want a measure as to how a column of first array varies with a column of second array. If yes, then that will be a scalar, otherwise, if you calculate the covariance matrix of two vectors, it is going to be a... matrix, obviously.
The following solution is based on the fact that covariance of two vectors is their correlation times the product of their standard deviations. More succinctly, for two random variables X and Y,
cov(X,Y)=corr(X,Y)*(sd(X)*sd*Y))
Thus, the solution to your question is:
pairCovariance=corr(X,Y).*(std(X).'*std(Y))

Is MATLAB row specific or column major?

In MATLAB, we can operate on both rows and columns of a matrix. What does it exactly mean by "row major" or "column major"?
It is important to understand that MATLAB stores data in column-major order, so you know what happens when you apply the colon operator without any commas:
>> M = magic(3)
M =
8 1 6
3 5 7
4 9 2
>> M(:)
ans =
8
3
4
1
5
9
6
7
2
I tend to think "MATLAB goes down, then across". This makes it easy to reshape and permute arrays without scrambling your data. It's also necessary in order to grasp linear indexing (e.g. M(4)).
For example, a common way to obtain a column vector inline from some expression that generates an array is:
reshape(<array expression>,[],1)
As with (:) this stacks all the columns on top of each other into single column vector, for all data in any higher dimensions.
But this nifty syntactic trick lets you avoid an extra line of code.
In MATLAB, arrays are stored in column major order.
It means that when you have a multi-dimensional array, its 1D representation in memory is such that leftmost indices change faster.
It's called column major order because for a 2D array (matrix), the first (leftmost) index is typically the row index, so since it changes faster than the second (next to the right) index, the 1D representation of the matrix is memory correspond to the concatenation of the columns of the matrix.

Assigning the different row to another matrix after comparing two matrices

i have two matrices
r=10,000x2
q=10,000x2
i have to find out those rows of q which are one value or both values(as it is a two column matrix) different then r and allocate them in another matrix, right now i am trying this.i cannot use isequal because i want to know those rows
which are not equal this code gives me the individual elements not the complete rows different
can anyone help please
if r(:,:)~=q(:,:)
IN= find(registeredPts(:,:)~=q(:,:))
end
You can probably do this using ismember. Is this what you want? Here you get the values from q in rows that are different from r.
q=[1,2;3,4;5,6]
r=[1,2;3,5;5,6]
x = q(sum(ismember(q,r),2) < 2,:)
x =
3 4
What this do:
ismember creates an array with 1's in the positions where q == r, and 0 in the remaining positions. sum(.., 2) takes the column sum of each of these rows. If the sum is less than 2, that row is included in the new array.
Update
If the values might differ some due to floating point arithmetic, check out ismemberf from the file exchange. I haven't tested it myself, but it looks good.