我想在matlab中计算两行的平均值 [英] I want to calculate the mean of two rows in matlab

查看:1019
本文介绍了我想在matlab中计算两行的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在matlab中有一个1028 x 18的矩阵.我想在Matlab中按列值,第3和第4等来计算第一行和第二行的平均值,并得到一个具有平均值的新矩阵.

I have a 1028 by 18 matrix in matlab.I want to calculate the mean of 1st and 2nd row by column values,3rd and 4th and so on in Matlab and get a new matrix with the mean values.

推荐答案

我认为您想计算每对行的按列平均值.将数组重塑为2 x 18 * 1028/2,计算平均值(按列进行操作),并将结果重塑为1028/2 x 18:

I think you want to calculate the column-wise mean of every pair of rows. Reshape the array to be 2 x 18*1028/2, calculate the mean (which operates column-wise), and reshape the result to be 1028/2 x 18:


>> x = rand(1028, 18);
>> result = reshape(x, 2, 1028/2*18);
>> result = mean(result);
>> result = reshape(result, 1028/2, 18);

一种快速测试,以证明矢量化解决方案的速度与成对的行之间的for循环相比:

A quick test to demonstrate the speed of vectorized solution compared to a for-loop over pairs of rows:


>> x = rand(1028, 18);
>> tic; result1 = zeros(1028/2, 18); for ii = 1:1028/2; result1(ii,:) = mean(x((2*ii-1):(2*ii),:)); end; toc;
Elapsed time is 0.022432 seconds.
>> tic; result2 = reshape(x, 2, 1028/2*18); result2 = mean(result2); result2 = reshape(result2, 1028/2, 18); toc;
Elapsed time is 0.000388 seconds.

这篇关于我想在matlab中计算两行的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆