1

In Matlab I can assign values inside arrays as follows.

a = [];
a(end+1, 1:2) = [1,2];
a(end,3:4) = [3,4];
a(end+1, 1:2) = [5,6];
a(end,3:4) = [7,8];

and so on. But in Python I can use the append command to append an array to the existing array. e.g.

a = []
a.append([1,2,3,4])
a.append([5,6,7,8])

My problem is I should assign the first two values at some point and the next two values in some other point as shown in my Matlab code. How can I do that?

3
  • 2
    an example of your expected output would be good. the python code you are showing would produce a=[[1,2,3,4]], while it seems from the matlab code that you want a=[[1,2],[3,4]] Commented Mar 25, 2018 at 13:10
  • I don't think the matlab code does what you think it does. Every time you run the command a(end+1, ...) you extend a by one more row; you're not assigning the values to the columns. Commented Mar 25, 2018 at 13:15
  • Sorry for the mistake. I have updated. Commented Mar 25, 2018 at 14:20

1 Answer 1

3

I think you're looking for extend:

a = []
a.append([1,2])  # [[1,2]]
a[-1].extend([3,4])  # [[1,2,3,4]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.