row 2 and row 1 difference - matlab

I have one column matrix with n rows of postitive and negative values . I want difference between row 2 and row 1 , row 3 and row 2 and so on. I should need the difference in a single column

How do you want to measure the difference?
You cannot get all those differences in one single column without using a suitable norm, try "help norm"
[n,m]= size(A);
d=zeros(1,n)
for k= 1:n-1
d(k) = norm(A(k,:))-norm(A(k+1,:));
end
The output is stored in the column-vector d.

Related

MATLAB: find means of other rows in a matrix without loop

I'm optimizing my codes. Now I have an MxN matrix, and I want to generate a mean MxN matrix which is the mean of other rows
for example: if i have matrix A:
1 2 3
3 4 5
2 3 2
In the new matrix B, I want each one is the mean of other rows.
mean(row2,row3)
mean(row1,row3)
mean(row1,row2)
I've think of many ways, but can't avoid a loop
for row=1:3
temp = A;
temp(row,:) = [];
B(row,:) = mean(temp);
end
any thoughts?
Simple with bsxfun -
B = (bsxfun(#minus,sum(A,1),A))./(size(A,1)-1)
The trick is to subtract the current row from the sum all rows with bsxfun in a vectorized manner, thus giving us the sum of all rows except the current one. Finally, get the average values by dividing them by the number of rows minus 1.

How do I identify the i,j position of a specific value on a Matlab matrix?

For example, I have a matrix A with 400 lines and 4000 columns which is mostly composed by zeros but have three ones. I do need to know exactly what position these ones occupy, let's say A(30,4000), A(400,3050) and A(50,200).
Simply do:
[row,col] = find(A);
This will give you the row and column locations of all non-zero entries. row would contain all of the row locations and col would contain all of the column locations as N x 1 vectors, where N is the number of non-zero elements. In your example, the above output would be equivalent to:
row = [30; 400; 50];
col = [4000; 3050; 200];
You just need to use find(.) function.
For example if you have:
q=[1 2 3;1 2 4];
[r c]=find(q==2)
in this case r includes rows and c includes columns of desired value.

Randomly selected matrix with no repetition of rows along with its index values in matlab

I have a 7x10 matrix, from this matrix I want to randomly select 4 rows without any repetition and this selection will include the index values of the selected rows. So, my question is: how to get a randomly selected matrix with no repetition of rows along with its index values of the original matrix from where it is selected?
Is this what you are after?
B = A(randperm(size(A,1),4),:)
Update: (thanks to federico)
idx = randperm(size(A,1),4);
B = A(idx,:)
Now idx will be a set of 4 integers between 1 and n, where n is the number of rows in A.
A(idx,:) gives you the elements in the rows represented by idx.

Find the mean of two rows in of a 42 by 4 matrix

I want to find the mean of 1st and 22nd row, the 2nd and 23rd row and so on of a 42-by-4 matrix. The first and 22nd rows are:
0 0 -30 -2.49000000000000
0 0 -30 -2.38000000000000
How can I find the mean of each column in these two rows?
MATLAB has a special syntax for indexing matrices, and you can learn about that by typing
help :
Now, suppose your matrix is
M = randn(42,4); %generating a random matrix with 42 rows and 4 columns
Then you can compute the mean of the desired rows using a simple add and average:
rowmeans = ( M(1:21,:) + M(22:end,:) ) / 2;
which will produce a matrix containing 21 rows and 4 columns, where each row is the desired average.
More generally, for averaging the top half and bottom half of a matrix that has an even number of rows:
rowmeans = ( M(1:end/2,:) + M(end/2+1:end,:) ) / 2;
You might also want to learn about the end keyword in MATLAB:
help end
If you want the mean of each colum of the two rows you can use something like
mean(t([1,22],:));
this will result to
0 0 -30.0000 -2.4350

How do I iterate through a matrix and retrieve the previous column in MATLAB?

I'm very new to MATLAB and not sure about this. I've a matrix
matrix = [1 2 3 4;8 9 5 6];
Now I want to iterate through the columns of the above matrix and retrieve the column
previous to the current one. So if while iterating, we're at column 2 then we should retrieve column 1.
Can someone please point me in the right direction? I tried
for v = matrix
disp(v-1)
end
but that didn't work. Any help will be greatly appreciated.
First, we need to find how many columns there are in your matrix:
m = [1,2,3,4;9,8,5,6]
[rows, cols] = size(m)
Next, we'll cycle through all the columns and print out current column:
for ii=1:1:cols
disp('current column: ')
m(:,ii) % the : selects all rows, and ii selects which column
end
If you wanted the previous column, rather than the current column:
for ii=1:1:cols
if ii == 1
disp('first column has no previous!')
else
disp('previous column: ')
m(:,ii-1) % the : selects all rows, and ii selects columns
end
end