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?

4

1 Answer 1

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]);
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Might have been a bad example. What about array2 = array1(132:279) I dont want to have to write them all individually

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.