0

In MATLAB its simple:

 array1 = [5,6,7,8];
 array2 = array1(2:3);

OUTPUT:

  array2 = [6,7]

How do I do this in CSharp?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
lsama
  • 199
  • 3
  • 13

1 Answers1

2

Arrays in c# start with index 0, so doing this will give you the same output as your example.

array1 = [5,6,7,8];
array2 = new Array[array1[1],array1[2]]

OUTPUT

array2 = [6,7]

EDIT because of this comment: Might have been a bad example. What about array2 = array1(132:279) I dont want to have to write them all individually – lsama

An easy way to do it is with a method like this.

array1 = [5,6,7,8];
array2 = new Array();

private void getThisIndexes(int firstIndex, int lastIndex){
  for(int i=0; i < array1.length; i++){
    if(i < firstIndex&& i >= lastIndex){
      array2.add(array1[i]);
    }
  }
}
H. Hakvoort
  • 161
  • 1
  • 2
  • 14
  • Might have been a bad example. What about array2 = array1(132:279) I dont want to have to write them all individually – lsama Oct 16 '17 at 10:30