MATLAB indexing for a matrix - matlab

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.

Related

How can i change indices of a matrix in Matlab?

My problem is i want to assign some numbers to indices of a matrix. For example if I remove first row and first column of a matrix, then in remaining matrix 3th row and 4 column would actually be 4th row and 5th column in the first place.
I can do it with Array1(Array2) , however my code will have many seperate recursions so it is frustrating to keep track of everything. So, is there an once and for all way to map original 1..n indices to remaining matrix even after I remove rows and columnsth
Thanks in advance
You can do something like this as per beaker's suggestion
originalMatrix = magic(4)
dimension = size(originalMatrix)
indexMatrix = zeros(dimension(1), dimension(2))
for i = 1:numel(indexMatrix)
indexMatrix(i) = i
end
and remove the required row and column from indexMatrix.

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.

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

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.

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))

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.