이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
code running for infinite while plotting my array for correlation
조회 수: 2 (최근 30일)
이전 댓글 표시
Bob
2019년 3월 24일
Hi,
I have problem as my code running for infinite time while I am plotting my arrays to achive coeffcient correlation.
here is my code:
for i=1:length(A)-300
Rx{i}= corrcoef(A (i:i+300), S(i:i+300));
Time_Rx=i;
B{1,i} = Rx(i)
C{1,i} = Time_Rx
end
(Aiming for plot Rx as a function over time)
plotting by using:
plot(B,C);
Can not figure out what is wrong, Any help would be appriciable.
댓글 수: 20
Walter Roberson
2019년 3월 24일
편집: Walter Roberson
2019년 3월 24일
I see no evidence that it is running for infinite time. It is, though, doing a sliding window of correlation coefficients, sliding one further each time, and that operation could take some time. Are you sure that you want to slide only one each time?
Note that you are writing into cell arrays, but it is not permitted to plot() a cell array. Especially since it is a cell of cells -- you write into Rx{i} but you B{1,i} = Rx(i) and notice that Rx(i) is a 1 x 1 cell, not the result of the correlation coefficient but rather the cell wrapping the result of the correlation coefficient.
Bob
2019년 3월 24일
편집: Bob
2019년 4월 5일
Thanks walter,
Attached snip from command window is the evidence for infinite run. it has been running since last more than an hour. I am not sure if I want only slide one each time. (How it should be implimented in either case).
If it is not permitted to plot() a cell array, what to do to achive my plot in this case?
B{1,i} = Rx(i)
C{1,i} = Rx
One small mistake it was
Rx{i}= corrcoef(A (i:i+300), S(i:i+300));
instead of
Rx{i}= corrcoef(A (i:i+300), B(i:i+300));
Kindly recommend some path for the plotting.
Walter Roberson
2019년 3월 24일
L = length(A) - 300;
B = zeros(2,2,L);
for K = 1 : L
B(:,:,K) = corrcoef(A(i:i+300), S(i:i+300));
end
C = 1 : L;
But it is not clear what you mean by plotting C (the index) vs B (a 2 x 2 array for each location).
It might make some sense to use
L = length(A) - 300;
B = zeros(L, 4);
for K = 1 : L
cc = corrcoef(A(i:i+300), S(i:i+300));
B(K, :) = cc(:);
end
C = 1 : L;
plot(C, B)
legend({'(1,1)', '(2,1)', '(1,2)', '(2,2)'})
Bob
2019년 4월 5일
Dear Walter,
There must be some confusion, I wanted to plot CC as correlation cofficent between the values A and S, over time.
Still its not achived.
Walter Roberson
2019년 4월 5일
When you calculate correlation coefficient between two columns, then the result is always 2 x 2.
Perhaps,
L = length(A) - 300;
B = zeros(L, 1);
for K = 1 : L
cc = corrcoef(A(i:i+300), S(i:i+300))
B(K) = cc(2,1);
end
plot(B);
Bob
2019년 4월 6일
Hi Again Walter,
When I use your inputs, following errors appeared:
Command window:
Warning: Colon operands must be real scalars.
Array indices must be positive integers or logical values.
cc = corrcoef(A(i:i+300), S(i:i+300))
Walter Roberson
2019년 4월 6일
L = length(A) - 300;
B = zeros(L, 1);
for i = 1 : L
cc = corrcoef(A(i:i+300), S(i:i+300))
B(i) = cc(2,1);
end
plot(B);
Bob
2019년 4월 6일
Wow. It worked well.
How can I make a Linear plot of CC values to visualize the all correlation points, either negative or positive correlation?
Walter Roberson
2019년 4월 6일
cc(1,1) is correlation of the section of A relative to itself. cc(2,2) is the correlation of the section of S relative to itself. cc(1,2) and cc(2,1) are the correlation of each relative to the other.
... I'm not sure what you are asking to extract and plot?
Bob
2019년 4월 6일
편집: Bob
2019년 4월 6일
What I mean is, can I see eaither cc(1,2) or cc(2,1), as a linear correlation plot by using all the correlation data point from cc ?? some thing like attached figure (as example).
Also if I can plot one more figure
for Z : diffrence between A and S.
Z=A-S;
figure(2)
plot(B,Z);
or
plot(cc,Z);
Is it possible ? I tried and error appears in command window:
Error using plot
Vectors must be the same length.
Walter Roberson
2019년 4월 8일
p = polyfit(B,1);
hold on
t = 1 : length(A)-300;
plot(t, polyval(p, t), 'b-');
Bob
2019년 4월 8일
It shows error:
Error using polyfit (line 44)
X and Y vectors must be the same size.
p = polyfit(B,1);
Walter Roberson
2019년 4월 8일
t = 1 : length(A)-300;
p = polyfit(t, B, 1);
hold on
plot(t, polyval(p, t), 'b-');
hold off
Bob
2019년 4월 8일
still have the error:
Error using polyfit (line 44)
X and Y vectors must be the same size.
p = polyfit(t, B, 1);
Bob
2019년 4월 8일
strange plotting now, see attached Image.
If its not achivable its fine.
Can you help me plot another para meter in contineuation with same code:
A = randn(1e3,1);
S = randn(1e3,1);
L = length(A) - 300;
B = NaN(length(A), 1);
for i = 1 : L
cc = corrcoef(A(i:i+300), S(i:i+300));
B(i) = cc(2,1);
end
plot(B);
Z=A-S;
figure(2)
plot(B,Z);
Can I plot B values at X axis and Z values at Y axis and in diffrent colors ?
Walter Roberson
2019년 4월 8일
A (nearly) horizontal line looks plausible to me given that data. Area above looks like it mostly balances area below, and the decreasing variance towards the right side would tend to anchor the endpoint of the line in values close to that range.
Walter Roberson
2019년 4월 8일
"Can I plot B values at X axis and Z values at Y axis and in diffrent colors ?"
When would the different colors be triggered?
Your B values increase and decrease irregularly like you see on the first plot. It would not seem to make sense to use them as the X axis values.
When you talk about "and in diffrent colors" my first interpretation was that you were wanting to plot two different lines, one for B, and one for Z, but then talking about plotting them on the X and Y axis would not seem to make any sense. The closest I can find to make sense of that would be to do something like
NA = length(A);
ZERO = zeros(NA, 1);
t = 1 : NA
plot3(t, B, ZERO, 'k', t, ZERO, Z, 'b');
which plots them with a common time base and with two different lines, each along a different axes.
Jan
2019년 4월 11일
Worked well.
@Bob: Please use flags only to informa admins about inappropriate contents like rudeness or spam. Thanks.
Bob
2019년 4월 12일
Hi Walter,
How can I avraged my data (A anS) in minutes (for 10 minutes) before I do correlation calculation, in above code ??
채택된 답변
BERGHOUT Tarek
2019년 4월 6일
편집: BERGHOUT Tarek
2019년 4월 6일
if you want to plot Rx , then you should plot B not C, and you can't plot B vs C in this example because C and B they dont have the same length ( dimensions are not the same), try this code , I hope that it is helpful:
clear all;
clc;
%%%
t=1:600;
A=0.25;
B=1.25;
f1=0.5;
f2=0.025;
S=A*sin(f1*t)+B*cos(f2*t)+wgn(size(t,1),size(t,2),3);
A=S+randn(size(S));
%%%
B=[];
for i=1:length(A)-300
Rx=corrcoef(A (i:i+300), S(i:i+300));
B=[B Rx];
end
plot(1:size(B,2),B);
댓글 수: 1
Bob
2019년 4월 6일
Dear berghout,
How can I make a Linear plot of Rx values to visualize the all correlation points, either negative or positive correlation?
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)