Summing in groups of rows - matlab

I need help making sums in matlab, I have a column with this aspect;
7
2
1
0
5
2
8
7
(...)
And now I want to sum those numbers in groups of 4 rows and get a new matrix with those numbers, for this example I will get a new column with:
10 (7+2+1+0)
22 (5+2+8+7)
(...)
Thx for helping

Using reshape allows you to make a 4xn-matrix out of your data. Doing so, you can use sum.
sum(reshape(x,4,numel(x)/4),1)'

Related

Accessing indexes as first columns of matrix in Matlab

I have data that is output from a computational chemistry program (Gaussian09) which contains sets of Force Constant data. The data is arranged with indexes as the first 2-4 columns (quadratic, cubic and quartic FC's are calculated). As an example the cubic FC's look something like this, and MatLab has read them in successfully so I have the correct matrix:
cube=[
1 1 1 5 5 5
1 1 2 6 6 6
.
.
4 1 1 8 8 8
4 2 1 9 9 9
4 3 1 7 7 7 ]
I need a way to access the last 3 columns when feeding in the indices of the first 3 columns. Something along the lines of
>>index=find([cube(:,1)==4 && cube(:,2)==3 && cube(:,3)==1]);
Which would give me the row number of the data that is index [ 4 3 1 ] and allow me to read out the values [7 7 7] which I need within loops to calculate anharmonic frequencies.
Is there a way to do this without a bunch of loops?
Thanks in advance,
Ben
You have already found one way to solve this, by using & in your expression (allowing you to make non-scalar comparisons).
Another way is to use ismember:
index = find(ismember(cube(:,1:3),[4 3 1]));
Note that in many cases, you may not even need the call to find: the binary vector returned by the comparisons or ismember can directly be used to index into another array.

matlab sort one column and keep respective values on second column

How do I just do a simple sort in matlab. I always have to use the excel link to import my data, sort it, then export back to matlab. This is annoying!!!
I have one matrix <10x10> and I want to sort the first column in descending order while keeping it's respective values on the second column. Matlab seems to just sort each column individually.
Example:
matrix a
5 4
8 9
0 6
7 3
matrix b (output)
0 6
5 4
7 3
8 9
The sortrows answer by #chaohuang is probably what you're looking for. However, it sorts based on all columns. If you only want to sort based on the first column, then you can do this:
% sort only the first column, return indices of the sort
[~,sorted_inds] = sort( a(:,1) );
% reorder the rows based on the sorted indices
b = a(sorted_inds,:);
Simply use b=sortrows(a); See here.

How can I flatten every n rows in matrix using Matlab?

I can easily flatten an entire matrix into one row using reshape(M,1,[]). However, this time I want to flatten every n rows into one row. Thus, if we start with 100 rows and n=10, we will end up with 10 rows.
e.g.
1 2 3
4 5 6
7 8 9
10 11 12
with n=2 changes into
1 2 3 4 5 6
7 8 9 10 11 12
Is there a simple way to do this?
Suppose your original matrix is m, then:
reshape(m',[6 2])'
produces the required output. I'll leave it to you to generalise to other cases; comment or post again if that causes you problems.
This should work.
reshape(M',l/n,n)'
Where n is what you've defined and l is the total elements in M.
EDIT: Made it one-liner

Removing rows with identical first column value in matlab

I have a cell matrix of size 10000 X 3 in Matlab and I would like to remove rows with the same value in the first column.
That is, if row i and row j have the same value in the first column, I'd like to delete both rows.
I should also say that there can be more than two rows with the same value in the first column and in that case, I'd like to delete all these rows.
How do I do it?
Thanks!
You can use the functions histc, unique and logical indexing to achieve what you want. Here's a small example.
a=randi(10,5,3) %#generate a sample random matrix
a =
5 3 5
5 7 10
7 7 4
8 2 6
8 2 3
[uniqVals,uniqIndx]=unique(a(:,1)); %# get unique values and corresponding indices of the first column of a
count=histc(a(:,1),uniqVals); %# get the bin counts of the elements (i.e., find which are repeated)
b=a(uniqIndx(count==1),:)
b =
7 7 4
Only the row with the non-repeated element is selected. Since you said that you have a cell matrix, simply covert it to a matrix using cell2mat before doing this.

How can I sort a 2-D array in MATLAB with respect to one column?

I would like to sort a matrix according to a particular column. There is a sort function, but it sorts all columns independently.
For example, if my matrix data is:
1 3
5 7
-1 4
Then the desired output (sorting by the first column) would be:
-1 4
1 3
5 7
But the output of sort(data) is:
-1 3
1 4
5 7
How can I sort this matrix by the first column?
I think the sortrows function is what you're looking for.
>> sortrows(data,1)
ans =
-1 4
1 3
5 7
An alternative to sortrows(), which can be applied to broader scenarios.
save the sorting indices of the row/column you want to order by:
[~,idx]=sort(data(:,1));
reorder all the rows/columns according to the previous sorted indices
data=data(idx,:)