フィルターのクリア

Index exceeds the number of array elements (0).

1 回表示 (過去 30 日間)
Chameleon
Chameleon 2020 年 11 月 23 日
コメント済み: Chameleon 2020 年 12 月 3 日
The code makes a new vector of values in the second column depending on the value 1-4 in the first column, for each iteration. outt is just for testing, in reality outt would contain different values in column 2 each time.
I would like it to work for all these scenarios:
outt = [1,100;2,200;3,300;4,400];
outt = [1,100;2,200;3,300];
outt = [1,100;2,200;3,300];
outt = [1,100];
but it only works for this scenario currently:
outt = [1,100;2,200;3,300;4,400];
here is the code:
%dessa ska vara utanför för att inte skriva över
statV_F = []
statV_A = []
statV_B = []
statV_D = []
outt = [1,100;2,200;3,300;4,400]
kolumn1 = outt(:,1)
%hitta om det finns 1-4:
no1 = find(1 == kolumn1);
no2 = find(2 == kolumn1);
no3 = find(3 == kolumn1);
no4 = find(4 == kolumn1);
%plocka ut raderna:
NO1 = outt(no1,:);
NO2 = outt(no2,:);
NO3 = outt(no3,:);
NO4 = outt(no4,:);
%göra vektorer som uppdaterar varje itertion:
statV_F = [statV_F NO3(2)]
statV_A = [statV_A NO3(2)]
statV_B = [statV_B NO3(2)]
statV_D = [statV_D NO4(2)]
  3 件のコメント
Chameleon
Chameleon 2020 年 11 月 23 日
the outt is an example of a typical input but the second column varies each time. I want to create 4 vectors that collects the data in the second column, the numbers in column one represents a category of data (1-4). Think of column 1 as one data type and column 2 as a value for that data type. The output for each datatype 1-4 should then be [iteration1_datacategory1 iteration2_datacategory1 iteration3_datacategory1] etc
Chameleon
Chameleon 2020 年 11 月 23 日
Sometimes there is only one datatype sometimes there are two...three..or four. But I check with find since datatype 3 might be missing etc

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

採用された回答

Shubham Khatri
Shubham Khatri 2020 年 12 月 3 日
Hello,
I have rewritten the code in the form of an if else statement.
clc
clear
%defining variables
statV_F = []
statV_A = []
statV_B = []
statV_D = []
outt = [1,100;2,200;3,300;4,400]
kolumn1 = outt(:,1)
k=length(outt)
if k==4
%finding value
no1 = find(1 == kolumn1);
no2 = find(2 == kolumn1);
no3 = find(3 == kolumn1);
no4 = find(4 == kolumn1);
%storing value
NO1 = outt(no1,:);
NO2 = outt(no2,:);
NO3 = outt(no3,:);
NO4 = outt(no4,:);
%Returning results
statV_F = [statV_F NO1(2)]
statV_A = [statV_A NO2(2)]
statV_B = [statV_B NO3(2)]
statV_D = [statV_D NO4(2)]
else if k==3
no1 = find(1 == kolumn1);
no2 = find(2 == kolumn1);
no3 = find(3 == kolumn1);
NO1 = outt(no1,:);
NO2 = outt(no2,:);
NO3 = outt(no3,:);
statV_F = [statV_F NO1(2)]
statV_A = [statV_A NO2(2)]
statV_B = [statV_B NO3(2)]
else if k==2
no1 = find(1 == kolumn1);
no2 = find(2 == kolumn1);
NO1 = outt(no1,:);
NO2 = outt(no2,:);
statV_F = [statV_F NO1(2)]
statV_A = [statV_A NO2(2)]
else k==1
no1 = find(1 == kolumn1);
NO1 = outt(no1,:);
statV_F = [statV_F NO1(2)]
end
end
end
Hope it helps
  1 件のコメント
Chameleon
Chameleon 2020 年 12 月 3 日
Thank you so much!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by