How can I plot sequential data of a matrix
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello, I've derived my data from a software and it gives the data in a sequential format. It's a 2-column matrix in which the first column is X and the second one is Temperature of that point. X starts from 0 to 0.2, then I have another 0 to 0.2 and this pattern repeats. The first X from 0 to 0.2 and its pertaining T are corrosponding values for first time step and the next one is for the next time step, ... . The number of rows are not fixed for every iteration and total number of rows are unknown. I want to plot X vs T for every time step. Does anyone have an idea for defining the matrix?
0 329.62
0.01 329.61
0.014 329.60
0.2 329.59
0.00 329.63
0.05 329.62
0.10 329.64
0.15 329.68
0.20 3295
0 329
...
채택된 답변
Star Strider
2021년 4월 18일
Try this:
A = [0 329.62
0.01 329.61
0.014 329.60
0.2 329.59
0.00 329.63
0.05 329.62
0.10 329.64
0.15 329.68
0.20 329.5
0 329];
is0 = find(A(:,1) == 0);
for k = 1:numel(is0)-1
idxrng = is0(k):is0(k+1)-1
segment{k} = A(idxrng,:);
end
figure
hold on
for k = 1:numel(segment)
plot(segment{k}(:,1), segment{k}(:,2), '.-')
end
hold off
grid
xlabel('X')
ylabel('T')
I have no idea how robust it will be for the full data set, however it works on the sample posted.
댓글 수: 8
Amir Mohammadi
2021년 4월 18일
편집: Amir Mohammadi
2021년 4월 18일
Hi,
First off, thank you very much for your response. I changed the A values to be actual data and it worked. The reason I wanted the segments separately was to calculate the average of these individual Temperature distributions over X, assuming a is the first segment and z the last one: Tavg=a+...+z/(number of data segments), leading to a single T vs X diagram, but when I want to calculate the average over the segment{k}(:,2) the result would be an average over the individual data set not over the entire data. Do you think I could use the segment{K}(:,2) to find the average?
thanks,
- Plot for actual data :
Average over the segment{k}(:,2):

As always, my pleasure!
Yes.
One option is to calculate the averages in the first loop:
for k = 1:numel(is0)-1
idxrng = is0(k):is0(k+1)-1;
segment{k} = A(idxrng,:);
segment_mean(k) = mean(segment{k}(:,2));
end
It is still possible to plot the mean values in the second loop, as well as plotting the segments themselves. (I have no idea how you are plotting the mean values in the second plot. I would suspect they would be a vertical line of points at some value of ‘X’.)
Hi,
The problem with my average plot was because I calculated it within second loop instead of the first one which I am not sure why lead to those bizzarre vertical lines. My issue still with the Mean Command is the fact that it calculate the average of the entire segment, leading to a horizontal line. I am looking for local average of the values, though, and by local, I mean determining the average of T at every single X. The result should be something like the plot below. I separated few segments one by one manually, then using Matlab's polyfit feature, calculated the average by summing up these individual graphs and dividing them to the number of plots. As you could see in the second plot the graphs are not completely accurate in comparison with the first picture in my first response since they are fitted, but it was the only solution I could think of and it as you may guess it is not possible to apply this approch to all data set I have. Anyways, thanks a lot as your responses were really helpful and I learnt many things from them.

If you want to calculate the mean values over an inconsistently-sampled array of data, it would be necessary to interpolate each segment dependent variable data to the same independent variable data. This ie straightforward with interp1, however it would need to be done on each segment (preferably in the first loop), and then stored in a separate array. Then it would be possible to calculate the mean values at every interpolated value.
Alright, I will try that. Thanks
As always, my pleasure!
That entire code would go something like this:
A = [0 329.62
0.01 329.61
0.014 329.60
0.2 329.59
0.00 329.63
0.05 329.62
0.10 329.64
0.15 329.68
0.20 329.5
0 329];
is0 = find(A(:,1) == 0);
intrp_x = linspace(0, 0.2, 10).'; % Interpolation X-Vector
for k = 1:numel(is0)-1
idxrng = is0(k):is0(k+1)-1;
segment{k} = A(idxrng,:);
segment_mean(k) = mean(segment{k}(:,2));
segment_intrp{k} = interp1(A(idxrng,1), A(idxrng,2), intrp_x); % Interpolate
end
intrp_means = mean(cell2mat(segment_intrp),2); % Mean Of Interpolated Values
figure
hold on
for k = 1:numel(segment)
plot(segment{k}(:,1), segment{k}(:,2), '.-', 'DisplayName',sprintf('Segment %d',k))
end
plot(intrp_x, intrp_means, '--k', 'DisplayName','Mean of Interpolated Values')
hold off
grid
xlabel('X')
ylabel('T')
legend('Location','best')
Experiment to get the result you want.
That's awesome. As always, Thank you!
As always, my pleasure!
추가 답변 (0개)
카테고리
도움말 센터 및 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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
