필터 지우기
필터 지우기

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by