Index exceeds the number of array elements (0).
1 次查看(过去 30 天)
显示 更早的评论
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)]
采纳的回答
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 Center 和 File Exchange 中查找有关 Results Review Process 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!