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
  • Have you tried anything? If you want C# you should so some C# attempts Commented Oct 16, 2017 at 9:32
  • 1
    var array2 = array1.Skip(1).Take(2).ToArray(); Commented Oct 16, 2017 at 9:33
  • In MATLAB, does changing the values in array2 also change the values in the array it was created from? Commented Oct 16, 2017 at 9:35
  • Might have been a bad example. What about array2 = array1(132:279) I dont want to have to write them all individually Commented Oct 16, 2017 at 10:31

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.