loop of Column Average
显示 更早的评论
I have a matrix of 1024 * 78.(datatype: double )
I want to average every two columns such that the resultant matrix must be 1024*39. (average of every two matrix)
how to get it done?
again for time being if i dont know the number of columns (due to huge dataset) and want to run a loop for averaging the columns for the similar case as mentioned about, what will be the changes in the code ?
采纳的回答
更多回答(1 个)
Eric Delgado
2022-4-19
function outMatrix = Fcn_twoColumnsMean(inMatrix)
arguments
inMatrix (1024,:) {mustBeNumeric} = randn(1024, 78)
end
NN = floor(width(inMatrix)/2);
outMatrix = zeros(1024, NN);
for ii = 1:NN
outMatrix(:,ii) = mean(inMatrix(:,2*ii-1:2*ii), 2);
end
end
5 个评论
Arvind Gauns
2022-4-19
Eric Delgado
2022-4-19
It's not a script, but a function. You have to save the code as Fcn_twoColumnsMean.m or Fcn_twoColumnsMean.mlx (if your are using Live Script).
Arvind Gauns
2022-4-19
Eric Delgado
2022-4-19
For sure! Especially if the script is becoming too big. So... you have to create modules for debugging purposes. But at the end of the day, it's your call! :)
Arvind Gauns
2022-4-19
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!