I want to assign multiple arrays of data simultaneously to different variable

8 views (last 30 days)
I want to assign multiple arrays of data simultaneously to different variable, but I think my syntax is wrong, Can anybody help me out here? A simple version of the code is gioven below.
D=[1 2 3 4 5 6 7 8 9 10];
[p q r]= [D(1:2) D(2:5) D(6:10)]
  2 Comments
TM Abir Ahsan
TM Abir Ahsan on 2 Jun 2022
@Stephen23 Yes, I understand your point. I think if these were single variables instead of array then my RHS inputs would have been assigned to the individual variables concatanated on the LHS. I was just curious as of why MATLAB could not do the same for arrays instead of single values on the RHS. The deal function is doing exactly what I was looking for here. Thanks for your comment :)

Sign in to comment.

Accepted Answer

Voss
Voss on 2 Jun 2022
Edited: Voss on 2 Jun 2022
D = [1 2 3 4 5 6 7 8 9 10];
[p q r] = deal(D(1:2), D(2:5), D(6:10))
p = 1×2
1 2
q = 1×4
2 3 4 5
r = 1×5
6 7 8 9 10
  1 Comment
TM Abir Ahsan
TM Abir Ahsan on 2 Jun 2022
Amazing! Thanks @Voss, I had no idea about the deal func. Thanks a bunch. This reduced a lot of line of my codes.

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!