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 ?

 채택된 답변

Davide Masiello
Davide Masiello 2022년 4월 19일
편집: Davide Masiello 2022년 4월 19일

0 개 추천

There's no need to use a for loop.
Try the code below.
clear,clc
A = rand(1024,78); % initial matrix of doubles
B = (A(:,1:2:end-1)+A(:,2:2:end))/2; % matrix of averages for each two columns
size(B)
ans = 1×2
1024 39

추가 답변 (1개)

Eric Delgado
Eric Delgado 2022년 4월 19일

0 개 추천

Hi @Arvind Gauns, just pass your input matrix to the function below. Hope it helps! :)
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
Arvind Gauns 2022년 4월 19일
' Error: Function definition are not supported in this context. Functions can only be created as local or nested functions in code files. '
this was the error encounted while using the above code.
Eric Delgado
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
Arvind Gauns 2022년 4월 19일
If i have to integrate everything within a script then will this still be relevent?
Eric Delgado
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
Arvind Gauns 2022년 4월 19일
in that case i need not need to call the function right ?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2022년 4월 19일

댓글:

2022년 4월 19일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by