Azzera filtri
Azzera filtri

Index exceeds the number of array elements (0).

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Chameleon
Chameleon il 23 Nov 2020
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 il 23 Nov 2020
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

Accedi per commentare.

Risposta accettata

Shubham Khatri
Shubham Khatri il 3 Dic 2020
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

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by