이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Slope of some groups of data separated by NaNs

채택된 답변
댓글 수: 15



















추가 답변 (1개)
Hi @Adi Purwandana,
After reviewing your comments, and analyzing the dataset data_mine.mat in matlab shown below
Name Size Bytes Class Attributes
dx 1x893 7144 double z 1x893 7144 double
You are right that it contains two variables, dx and z. As you mentioned that dx having groups of values separated by NaNs, first import the .mat. file to access the variables as shown in your code snippet. Then, split the dx values into separate groups based on the NaN values. For each group, fit a linear regression model to determine the slope and then visualize each group along with its corresponding slope line. Here is a full code example in MATLAB that accomplishes this:
% Load the dataset
load('data_mine.mat');
% Initialize variables
dx_groups = {};
z_groups = {};
current_group_dx = [];
current_group_z = [];
% Split dx and z into groups based on NaN in dx
for i = 1:length(dx)
if isnan(dx(i))
if ~isempty(current_group_dx) % Only save if current group is not empty
dx_groups{end + 1} = current_group_dx; %#ok<AGROW>
z_groups{end + 1} = current_group_z; %#ok<AGROW>
current_group_dx = []; % Reset for next group
current_group_z = []; % Reset for next group
end
else
current_group_dx(end + 1) = dx(i); %#ok<AGROW>
current_group_z(end + 1) = z(i); %#ok<AGROW>
end
end
% Add last group if it exists
if ~isempty(current_group_dx)
dx_groups{end + 1} = current_group_dx; %#ok<AGROW>
z_groups{end + 1} = current_group_z; %#ok<AGROW>
end
% Initialize figure for plotting figure; hold on;
% Analyze each group and plot results
for g = 1:length(dx_groups)
% Extract current group
x = dx_groups{g};
y = z_groups{g};
% Perform linear regression to find slope
p = polyfit(x, y, 1); % p(1) is the slope, p(2) is the intercept % Generate x values for plotting the fitted line
x_fit = linspace(min(x), max(x), 100);
y_fit = polyval(p, x_fit); % Plot original data points
plot(x, y, 'o', 'DisplayName', ['Group ' num2str(g)]); % Plot the fitted line
plot(x_fit, y_fit, 'LineWidth', 2, 'DisplayName', ['Slope: ' num2str(p(1))]);
end% Finalize plot settings
xlabel('dx');
ylabel('z');
title('Slope Analysis of Groups in dx');
legend show;
grid on;
hold off;
% Display slopes for each group in command window
for g = 1:length(dx_groups)
fprintf('Group %d: Slope = %.4f\n', g, polyfit(dx_groups{g}, z_groups{g},
1));
end
Please see attached.


As you will notice that the code starts by loading the data_mine.mat file.It loops through dx, collecting values until a NaN is encountered. Each segment is stored in separate cell arrays (dx_groups and z_groups). For each group of data points, a linear fit is performed using polyfit, which returns coefficients for a linear equation (y = mx + b). For more information on this function, please refer to polyfit
Each group’s data points and corresponding fitted line are plotted. The slopes are displayed in the legend and printed to the console. Adjust visualization parameters (like colors or markers) based on your preferences or specific requirements.
Hope this helps.
Please let me know if you have any further questions.
댓글 수: 1
참고 항목
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 (한국어)


