Hello, how can I do a (for loop) for this equation to find more than one value of the CF ? Specifically, I mean more than one value for (xm)

조회 수: 1 (최근 30일)
function [CF]= Coherence_Factor(xm,N);
CF = ((abs (sum(xm))).^2) / (N* sum(abs(xm.^2)))
end
%Function call window
xm= [-1:4],N=length(xm);
xm=[-5:-2],N=length(xm);
xm=[-2:1],N=length(xm)
[CF]= Coherence_Factor(xm,N)

답변 (2개)

VBBV
VBBV 2022년 12월 31일
xm= {[-1:4], [-5:-2],[-2:1]};
xm = 1×3 cell array
{[-1 0 1 2 3 4]} {[-5 -4 -3 -2]} {[-2 -1 0 1]}
for k = 1:numel(xm)
N=length(xm(k));
CF(k)= Coherence_Factor(xm(k),N);
end
CF
CF = 1×3
2.6129 3.6296 0.6667
function [CF]= Coherence_Factor(xm,N);
CF = ((abs (sum(cell2mat(xm)))).^2) ./ (N* sum(abs(cell2mat(xm).^2)));
end

Voss
Voss 2023년 1월 1일
xm = {-1:4, -5:-2, -2:1};
Nx = numel(xm);
CF = zeros(1,Nx);
for k = 1:Nx
N = numel(xm{k});
CF(k) = Coherence_Factor(xm{k},N);
end
CF
CF = 1×3
0.4355 0.9074 0.1667
function CF = Coherence_Factor(xm,N)
CF = abs(sum(xm)).^2 / (N*sum(abs(xm.^2)));
end
Note that if the xm are always guaranteed to be real-valued (as opposed to complex-valued), then the abs() calls are unnecessary:
xm = {-1:4, -5:-2, -2:1};
Nx = numel(xm);
CF = zeros(1,Nx);
for k = 1:Nx
N = numel(xm{k});
CF(k) = Coherence_Factor(xm{k},N);
end
CF
CF = 1×3
0.4355 0.9074 0.1667
function CF = Coherence_Factor(xm,N)
CF = sum(xm).^2 / (N*sum(xm.^2));
end
And if N is always supposed to be the number of elements in xm, you can define N inside the function Coherence_Factor and avoid having the second input argument:
xm = {-1:4, -5:-2, -2:1};
Nx = numel(xm);
CF = zeros(1,Nx);
for k = 1:Nx
CF(k) = Coherence_Factor(xm{k});
end
CF
CF = 1×3
0.4355 0.9074 0.1667
function CF = Coherence_Factor(xm)
N = numel(xm);
CF = sum(xm)^2 / (N*sum(xm.^2));
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 1월 1일
I wonder if it is supposed to be sum(abs(xm).^2) or abs(sum(xm.^2)) ?
The current sum(xm) having priority has different results if values could be a mix of negative and positive

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by