How to choose data with conditions?

조회 수: 17 (최근 30일)
Mario
Mario 2022년 8월 17일
댓글: Mario 2022년 8월 17일
Hello!! I'm new in programming world and I’m trying to select the data of a column with some condition; I need to select only when the data is increasing in the column, and when is decreasing in the same column. I did it in excel with: =IF((BA3-BA2)<0,BA2+(BA3-BA2)) --- for decreasing, and =IF((BA3-BA2)>0,BA2+(BA3-BA2)) for increasing. But I’m not able to do it in mat lab with the entire column.
If somebody can help me I really appreciative!
data = xlsread('Data1.xlsx')
data = 213×1
109.4632 109.3436 108.9267 108.2206 107.2488 106.1058 104.9447 103.8366 102.7987 101.7733

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 8월 17일
This can be done a few different ways. MATLAB has several very efficient built-in functions to compute any value change dynamics. In your exercise, you would need to take the following steps:
Step 1. Data import
D = readmatrix('Data1.xlsx')'
Step 2. Compute the data chaneg dynamics
dD = diff(D);
Step 3. Find out if the data is increasing or decreasing order
IDX1 = find(dD>0); % Locate Indices for increase
IDX2 = find(dD<0); % Locate Indices for decrease
Step 4. Do something ...
  댓글 수: 1
Mario
Mario 2022년 8월 17일
Thanks so much, it works. It is possible to keep help me it would be great. Now that I located the increasing and decreasing data, I need to sum all the increasing data and all the decreasing data. I'm not able to do it when I sum the location (IDX1 or IDX2).
Also, I was trying to identify in the plot (with some symbols) the increasing and decreasing data, without successful results. How can I do it??
Thanks again, have a nice day
Mario N.L.

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

추가 답변 (1개)

Cris LaPierre
Cris LaPierre 2022년 8월 17일
Create a logical array and use that to select the values you want. See Ch 11 of MATLAB Onramp.
A simple example
A = [109.4632
109.3436
108.9267
108.2206
107.2488];
% logical array of locations where sequential rows are increasing or
% decreasing (will be one row shorter than A)
d = diff(A);
ind = d~=0;
% Create a new vector based on your formula: next value is current value
% plus difference of next row and current row
B = A([ind;false]) + d(ind)
B = 4×1
109.3436 108.9267 108.2206 107.2488

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by