Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6969247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:33:25+00:00 2026-05-27T16:33:25+00:00

If I have a short list (let’s say two or three elements) I would

  • 0

If I have a short list (let’s say two or three elements) I would like to have function that split it in several variables. Something like that:

li=[42 43];
[a b]=split(li)
 --> a=42
 --> b=43

I am looking for some way to make my code shorter in matlab. This one would be nice in some situations For instance:

positions=zeros(10,3);
positions= [....];
[x y z]=split(max(positions,1))

instead of doing:

pos=max(positions,1)
x=pos(1);
y=pos(2);
z=pos(3);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:33:25+00:00Added an answer on May 27, 2026 at 4:33 pm

    The only way I know of to do this is to use deal. However this works with cell arrays only, or explicit arguments in to deal. So if you want to deal matrices/vectors, you have to convert to a cell array first with num2cell/mat2cell. E.g.:

    % multiple inputs
    [a b]   = deal(42,43) % a=2, b=3
    [x y z] = deal( zeros(10,1), zeros(10,1), zeros(10,1) )
    
    % vector input
    li  = [42 43];
    lic = num2cell(li);
    [a b]=deal(lic{:}) % unforunately can't do num2cell(li){:}
    % a=42, b=43
    
    % matrix input
    positions  = zeros(10,3);
    % create cell array, one column each
    positionsc = mat2cell(positions,10,[1 1 1]);
    [x y z]    = deal(positionsc{:})
    

    The first form is nice (deal(x,y,...)) since it doesn’t require you to explicitly make the cell array.

    Otherwise I reckon it’s not worth using deal when you have to convert your matrices to cell arrays only to convert them back again: just save the overhead. In any case, it still takes 3 lines: first to define the matrix (e.g. li), then to convert to cell (lic), then to do the deal (deal(lic{:})).

    If you really wanted to reduce the number of lines there’s a solution in this question where you just make your own function to do it, repeated here, and modified such that you can define an axis to split over:

    function varargout = split(x,axis)
    % return matrix elements as separate output arguments
    % optionally can specify an axis to split along (1-based).
    % example: [a1,a2,a3,a4] = split(1:4)
    % example: [x,y,z] = split(zeros(10,3),2)
    if nargin < 2
        axis = 2; % split along cols by default
    end
    dims=num2cell(size(x));
    dims{axis}=ones([1 dims{axis}]);
        varargout = mat2cell(x,dims{:});
    end
    

    Then use like this:

    [a b]  = split([41 42])
    [x y z]= split(zeros(10,3), 2) % each is a 10x1 vector
    [d e]  = split(zeros(2,5), 1)  % each is a 1x5 vector
    

    It still does the matrix -> cell -> matrix thing though. If your vectors are small and you don’t do it within a loop a million times you should be fine though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a short list of strings, that can contain duplicates: <A,
Let's say I have a List object and an iterator for that list. Now
We have a list of (let's say 50) reports that get dumped into various
This is a long shot, I know... Let's say I have a collection List<MyClass>
Let's say I have a bean like below. class Customer{ private String code; private
Let's say I have an unsorted list of four objects: [B, C, A, D]
I would like to add an element to a list that preserve the order
Let's say I have the following list: var someList = new List<SomeObject>(); Which has
Let's say I have books, each belonging to an author. I'd like to print
Let's say I have two SQL tables: QYZ: id, uid<user id, FK*>, somedatacolumn USERS:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.