Split array in 3 groups by using one column values

Hi,
I would like to split my data in 3 groups based on the values in axis 1
x = < 25 i would like to get a 0
x = > 25 < 419 i would like to get a 1
x = > 419 i would like to get a 2
how can I do this?
% Clear memory
clear; close all;
% Load data
T=readtable('SC01001 (2018-11-23)15sec.csv','PreserveVariableNames',true)
x1=T(:,1); %Date
x2=T(:,2); % Time
y1=T(:,3); % Axis 1
if
y1 <25
y1 = 0
elseif
y1 25>y1<419
y1 = 1
elseif
y1 >419
y1 = 2
end

 採用された回答

Marta G
Marta G 2020 年 2 月 24 日
編集済み: Marta G 2020 年 2 月 24 日

0 投票

You shouldnt use a vector in a if condition.
In your case, it would be more effective to use this
loc=find(y1<25);
y(loc)=0;
loc1=find(y1>25 & y1<419);
y(loc1)=1;
loc2=find(y1>419);
y(loc2)=2;
If you are new to matlab you might understand the problem better with a for loop.
for i=0:length(y1)
if(y1(i)<25)
y(i)=0;
elseif (y1(i)>25 & y1(i)<419)
y(i)=1;
else
y(i)=2;
end
end
I prefer to not rewrite any variable i am using until it is completely necessary.
Make sure you really need those intervals and consider what you have to do if y1=25 since in your original code it would not enter any if else condition

7 件のコメント

Ankit
Ankit 2020 年 2 月 25 日
small correction in the first line of the for loop, i think by mistake you wrote 0 instead of 1.
Annick
Annick 2020 年 2 月 25 日
I tried both of them,
at the first one, while using the location method, I get the following error message:
Undefined operator '<' for input arguments of type 'table'.
Error in main (line 10)
loc=find(y1<25);
but while using the for loop I get the following error message:
Error using main (line 11)
Subscripting into a table using one subscript (as in t(i)) or three or more subscripts (as in t(i,j,k)) is not
supported. Always specify a row subscript and a variable subscript, as in t(rows,vars).
How can I solve this problem?
Ankit
Ankit 2020 年 2 月 25 日
could you post the code which throws above error when you are using for loop? What is "t" in your code?
As based on your question, solution provided by Marta G will not throw any error.
Annick
Annick 2020 年 2 月 25 日
編集済み: Annick 2020 年 2 月 25 日
This is everything i have
% Clear memory
clear; close all;
% Load data
T=readtable('SC01001 (2018-11-23)15sec.csv','PreserveVariableNames',true)
x1=T(:,1); %Date
x2=T(:,2); % Time
y1=T(:,3); % Axis 1
for i=1:length(y1)
if(y1(i)<25)
y(i)=0;
elseif (y1(i)>25 & y1(i)<419)
y(i)=1;
else
y(i)=2;
end
end
The error code it gaves is
Error using tabular/length (line 211)
Undefined function 'length' for input arguments of type 'table'. Use the HEIGHT, WIDTH, or SIZE functions
instead.
Error using main (line 12)
Subscripting into a table using one subscript (as in t(i)) or three or more subscripts (as in t(i,j,k)) is not
supported. Always specify a row subscript and a variable subscript, as in t(rows,vars).
for i=1:length(y1)
Ankit
Ankit 2020 年 2 月 25 日
編集済み: Ankit 2020 年 2 月 25 日
I didn't see any error in the above code. Are you able to readtable ? you can see y1 in workspace? you attached xlsx file why not attached csv file? this is how y look when i use your attach excel sheet
Annick
Annick 2020 年 2 月 25 日
Everything above the for loop works for me. This is the screen I get. Even when I try the xlsx file, i keep getting the same error message
Marta G
Marta G 2020 年 2 月 25 日
編集済み: Marta G 2020 年 2 月 25 日
Hello,
Thank you Ankit for your correction
There is a step missing:
x1=T(:,1); %Date
x2=T(:,2); % Time
y1=table2array(T(:,3)); % Axis 1
for i=1:length(y1)
if(y1(i)<25)
y(i)=0;
elseif (y1(i)>25 & y1(i)<419)
y(i)=1;
else
y(i)=2;
end
end
Make sure in future problems that you read the error and understand what it means, as that line missing is easily solvable. If you need matrix or vectors instead of table you can use table2array: https://es.mathworks.com/matlabcentral/answers/380821-how-to-convert-table-to-matrix

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2020 年 2 月 24 日

編集済み:

2020 年 2 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by