I have a matrix M which is a 29 x 18 double, something like this:
1 1 1 ...
2 1 1 ...
3 1 2 ...
2 2 2 ...
2 1 3 ...
3 1 3 ...
1 3 3 ...
...
For each possible pair of two columns in M, I want to calculate the number of times the values of the same row between two columns are identical. Take column 1 and 2 for instance, the number of times values of the same row are identical is 2 since M(1,1) = M(1,2) and M(4,1) = M(4,2). This calculation is repeated 18 time for each column as each column is paired with each of the total number of columns, including itself. Thus, the output (called N) would be 18 x 18 matrix with the each value indicating how many instances the values of the same row from the original two corresponding columns are identical. Something like this
29 4 5 3 ...
4 29 6 0 ...
5 6 29 7 ...
...
Since N(2,1) = 4, this would indicate that column 1 and column 2 matrix M have 4 matching values of the same row.
How do I do this?
you can do a double for loop like that:
result = zeros(18);
for i = 1:18
for j = 1:18
result(i,j) = nnz(M(:,i) == M(:,j));
end
end
Let's say I have the following matrix:
j =
1 2 3 4
1 2 3 4
5 6 7 8
5 6 7 8
I would like to get back the following matrix:
z =
3 4
4 4
My experience with the max command hasn't yielded a result that resembles z, it appears that the max function turns the argument into a column vector.
It appears you want the row and column indices of all occurrences of the maximum computed across all dimensions (i.e. the maximum is a single value, which possibly appears in several entries). Then:
[rows, cols] = find(j==max(j(:)));
result = [rows cols];
Basically the sum function calculate the sum of the columns, that is to say if we have a 4x4 matrix we would get a 1X4 vector
A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
sum(A)
ans =
34 34 34 34
But if I want to get the Summation of the rows then i have 2 methods, the first is to get the transpose of the matrix then get the summation of the transposed matrix,and finally get the transpose of the result...., The Second method is to use dimension argument for the Sum function "sum(A, 2)"
A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
sum(A,2)
ans =
34
34
34
34
The problem is here I cannot understand how this is done, If anyone could please tell me the idea/concept behind this method,
It's hard to tell exactly how sum internally works, but we can guess it does something similar to this.
Matlab stores matrices (or N-dimensional arrays) in memory using column-major order. This means the order for the elements in memory for a 3 x 4 matrix is
1 4 7 10
2 5 8 11
3 6 9 12
So it first stores element (1,1), then (1,2), then (13), then (2,1), ...
In fact, this is the order you use when you apply linear indexing (that is, index a matrix with a single number). For example, let
A = [7 8 6 2
9 0 3 5
6 3 2 1];
Then A(4) gives 8.
With this in mind, it's easy to guess that what sum(A,1) does is traverse elements consecutively: A(1)+A(2)+A(3) to obtain the sum of the first column, then A(4)+A(5)+A(6) to sum the second column, etc. In contrast, sum(A,2) proceeds in steps of size(A,1) (3 in this example): A(1)+A(4)+A(7)+A(10) to compute the sum of the first row, etc.
As a side note, this is probably related with the observed fact that sum(A,1) is faster than sum(A,2).
I'm really not sure what you are asking. sum takes two inputs, the first of which is a multidimensional array A, say.
Now let's take sA = size(A), and d between 1 and ndims(A).
To understand what B = sum(A,d) does, first we find out what the size of B is.
That's easy, sB = sA; sB(d) = 1;. So in a way, it will "reduce" the size of A along dimension d.
The rest is trivial: every element in B is the sum of elements in A along dimension d.
Basically, sum(A) = sum(A,1) which outputs the sum of the columns in the matrix. 1 indicates the columns. So, sum(A,2) outputs the sum of the rows in the matrix. 2 indicating the rows. More than that, the sum command will output the entire matrix because there is only 2 dimensions (rows and columns)
I have a N X 2 matrix which I would like to multiply the second column by a certain number.
The matrix looks like this:
1 0
6 0
7 0
8 0
4 0.0173000000000000
5 0.0269000000000000
3 0.0720000000000000
2 0.883800000000000
But I would like to multiply just the second column by a certain number without affecting the first column. How do I do that in MATLAB?
a(:, 2) = a(:,2)*2
where a is your matrix.
A=[ 2 4 8 20 0 0;
1 3 6 18 22 0;
0 3 5 8 18 20]
and then from the above matrix, I want to account average of max value of every rows.
so I wish the result :
result=average(20+22+20)=20,67
thankf for your help.
[C,I] = max(...) finds the indices of the maximum values of A, and returns them in output vector I. If there are several identical maximum values, the index of the first one found is returned.
for your problem
maxValues= max(A,[],2)
result=Mean(maxValues)