how to calculate avrage of 1st 5 rows then next 5 row likewise continue in matlab - matlab

I have .xlsx file n rows and m column. I want to calculate average of 1st 5 rows then next 5 rows likewise ( each 5 interval average) continue for my data.

If A is your matrix, what about:
m=[];
for ii=1:5:20
m(end+1)=mean(mean(A(ii:ii+4,:)));
end
does it work for you?

Here is a "hack" that should be pretty fast if A is big:
m = mean(kron(speye(size(A,1)/5),ones(1,5))*A/5,2);

You can use reshape to get blocks of 5 rows in the first dimension. If A is your matrix
m = squeeze(mean(reshape(A,5,[],size(A,2)),1));
The code works as follows
reshape the matrix to get the blocks of 5 rows in the first dimension
Compute the mean over the blocks of 5 rows.
After the mean, the first dimension is a singleton so it is better to squeeze it so the output is a 2D matrix.

Related

average every n rows in a complex matrix

I have a big complex single matrix (9040 X 23293).
Because this matrix holds to much data for me, I want to average every n rows. For example, n can be 10 and the new matrix will be 904 X 23293.
I tried to use reshape but it does not work on complex numbers.
I would love to get some help.
Thanks,
Lauren
Thanks.
Laurn
Reshape works on complex numbers. As you did not share the code, I do'nt know what is the problem. Anyhow, if the number of rows is not multiple of 10, you can reshape first 10 * n rows and add the average of the remain rows. You can find the general solution in the following for the given complex matrix m:
fixed_num_rows = fix(size(m,1)/n);
means = mean(reshape(m(1:(fixed_num_rows * n),:), fixed_num_rows, n * size(m,2)),2);
means = [means; mean(mean(m((fixed_num_rows * n + 1):size(m,1),:)))];

How do I efficiently multiply every 2 columns and sum the row

Maybe I should just go with a for loop but I want to see if there is a more efficient/faster way to do it.
I have a matrix of numbers, let's say 10x10. I want to multiply 1,1 by 1,2, then 1,3 times 1,4, etc and then sum those results for row 1. Then move to the next row and do the same thing. The end result would be a vector of 10.
It is possible for this matrix to be 1000x1000 so I want it to be as fast as possible. Thanks!
I would use
v = sum(M(:,1:2:end-1).*M(:,2:2:end),2);
Here M(:,1:2:end-1).*M(:,2:2:end) does multiplication: every element of an odd-numbered column of M is multiplied by its neighbor to the right. (This assumes even number of columns, otherwise the process you described is ill-defined.) Then every row is added up by the sum command.
On my computer, doing this for a 1000 by 1000 matrix takes 0.04 seconds.

how can i deal with empty matrix: 0-by-any number resulting from simulation?

I have made a simulation and the result each time of the simulation is a matrix and i choose a certain row from the matrix, so if the simulation run=500, i'll have a 500 matrix and ,the row i choose each time will be (at the end of the simulation) 500 rows [one row from the first matrix...last row from the last matrix]...
the problem is some times a matrix dose not contain the certain row i want , the answer is for example empty matrix: 0-by-6
i want to ignor this answer
Note: the row i choose is not necessary to be exist in all matrices
so if the run=600 , result in 600 matrix , the row i choose maybe =400 only and the other 200 will be zero
the simulation STOP when the result is empty matrix: 0-by-any number
I use Matlab
you can use isempty to detect empty arrays, for example
a=zeros(0,5)
isempty(a)
a =
Empty matrix: 0-by-5
ans =
1
For when the index exceeds matrix dimensions, you can add a condition that tests the size of your matrix, specifically, how man rows using size(m,1)
So all together, in your for loop you can code something like:
for n=1:blah
if ~isempty(M) % continue if matrix is non-empty
if size(M,1)<=n % continue if index doesn't exceeds matrix dimensions
....
....

How to change elements in matrices using MATLAB

Starting wish a 7x4 binary matrix I need to change a random bit in each column to simulate error. Have been trying to no avail.
A very straightforward way to do this is to use a for loop. It might not be the most efficient approach in MATLAB, but it's probably good enough considering your data set is so small.
Iterate through each of the four columns. On each iteration, randomly chose a number from 1 to 7 to represent the row in that column that you have selected to change. Finally, flip the bit at that row/column. The following code does just this. Assume that "A" is a binary matrix with 7 rows and 4 columns
for col=1:4; %// Iterate through each column
row = ceil(7*rand()); %// Randomly chose a number from 1 to 7 to represent row
A(row,col) = ~A(row,col); %// Flip the bit at the specified row/col
end
Another possibility is to create 4 random numbers in one call, and assign in a vectorized fashion:
rowNumbers = randi(4,[1 4])
A(rowNumbers,:) = ~A(rowNumbers,:);

Using ranges in Matlab/Octave matrices

Let's say I want to create an 100x100 matrix of which every row
contains the elements 1-100
A = [1:100; 1:100; 1:100... n]
Obviously forming a matrix is a bad idea, because it would force me to
create 100 rows of range 1:100.
I think I could do it by taking a 'ones' array and multiplying every
row by a vector... but I'm not sure how to do it
a = (ones(100,100))*([])
??
Any tips?
You can use the repeat matrix function (repmat()). You code would then look like this:
A = repmat( 1:100, 100, 1 );
This means that you're repeating the first argument of repmat 100 times vertically and once horizontally (i.e. you leave it as is horizontally).
You could multiply a column vector of 100 1s with a row vector of 1:100.
ones(3,1)*(1:3)
ans =
1 2 3
1 2 3
1 2 3
Or you could use repmat ([edit] as Phonon wrote a few seconds before me [/edit]).
Yes, repmat is the easy solution, and even arguably the right solution. But knowing how to visualize your aim and how to create something that yields that aim will give long term benefits in MATLAB. So try other solutions. For example...
cumsum(ones(100),2)
bsxfun(#plus,zeros(100,1),1:100)
ones(100,1)*(1:100)
cell2mat(repmat({1:100},100,1))
and the boring
repmat(1:100,100,1)