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

  • Home
  • SEARCH
  • 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 8755937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:57:14+00:00 2026-06-13T13:57:14+00:00

I’m trying to group by column datas from a matrix. The data are extracted

  • 0

I’m trying to “group by column” datas from a matrix.

The data are extracted from a database, and the matrix looks like that :

'2012-04-26'    'USD'    'BRL'    [    1.8894]
'2012-04-26'    'USD'    'IDR'    [      9185]
'2012-04-26'    'USD'    'INR'    [   52.5350]
'2012-04-26'    'USD'    'MXN'    [   13.2337]
'2012-04-26'    'USD'    'PEN'    [    2.6505]
'2012-04-26'    'USD'    'SGD'    [    1.2412]
'2012-04-26'    'USD'    'TRY'    [    1.7643]
'2012-04-27'    'USD'    'BRL'    [    1.8846]
'2012-04-27'    'USD'    'IDR'    [      9189]
'2012-04-27'    'USD'    'INR'    [   52.5600]
'2012-04-27'    'USD'    'MXN'    [   13.0147]
'2012-04-27'    'USD'    'PEN'    [    2.6395]
'2012-04-27'    'USD'    'SGD'    [    1.2385]
'2012-04-27'    'USD'    'TRY'    [    1.7600]

(this is a cell-array)

What I want to do is to group all datas by date (1st row) and then have one column for each value, like this :

'2012-04-26'    [    1.8894]    [      9185]    [   52.5350]    [   13.2337]    [    2.6505]    [    1.2412]    [    1.7643]    
'2012-04-27'    [    1.8846]    [      9189]    [   52.5600]    [   13.0147]    [    2.6395]    [    1.2385]    [    1.7600]

where each row represents a currency pair (USD/BRL, USD/IDR, USD/INR, …)

Note that, for each date, they are exacltly the same number of lines (currency pairs) in the extracted data.

Is there an elegant (and fast) way to achieve this in Matlab ?

Thanks,

  • 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-06-13T13:57:14+00:00Added an answer on June 13, 2026 at 1:57 pm

    Given that you emphasize that speed is important in the question, I propose the following solution:

    %# Build an example cell array 
    D = cell(6, 4);
    for t = 1:3; D{t, 1} = '2012-04-26'; D{t, 2} = 'A'; D{t, 3} = 'A'; D{t, 4} = t; end;
    for t = 4:6; D{t, 1} = '2012-04-27'; D{t, 2} = 'A'; D{t, 3} = 'A'; D{t, 4} = t; end;
    
    %# My Solution
    X = [datenum(D(:, 1), 'yyyy-mm-dd'), cell2mat(D(:, 4))];
    [UniqueDate, ~, Index] = unique(X(:, 1));
    NumObsPerDay = sum(Index == 1);
    NumDay = length(UniqueDate);
    Soln = [UniqueDate, reshape(X(:, 2), NumObsPerDay, NumDay)'];
    

    In the first line, I extract the data that is important into a numeric array. Numeric arrays are MUCH faster to operate on than cell arrays as a single element takes up much less memory. To deal with the date strings, I convert them in this first step into matlab numerical date format. If you plan on using Matlab much, I suggest you get familiar with the numerical date format as it is much more flexible than working with strings – for example, you can perform any kind of arithmetic you desire to the numerical date format.

    In the second line, I get a unique list of dates, and an index.

    In the third and fourth line I use the index to obtain the number of days for which you have data, and the number of observations for each day. CAUTION: The line NumObsPerDay = sum(Index == 1); implicitly assumes that you have the same number of observations (ie other currencies) for each day. However, you state in the question that this is the case, so I’m taking you at your word 🙂

    In the fifth line, I create a numerical matrix that has the format you desire. The first column is the unique date vector obtained in line 2, and I’ve obtained the remaining columns by reshaping the data in X. CAUTION: This line implicitly assumes that the ordering of the currencies in your cell array are identical for each day. Again, I’ve made this assumption because it is true in your sample data and you stated you wanted a fast solution.

    FINAL CAUTION: If either of the assumptions made above are violated then this code will fail, or your data will get mixed up. In other words, if you’re certain that all your data conforms to the sample you provided, then this solution should serve, and should also be fast. But if you’re not certain, then this is not a good solution for you.

    ps if you want to see the dates in string format again, just use datestr(Soln(:, 1), 'yyyy-mm-dd');

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have a view passing on information from a database: def serve_article(request, id): served_article

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.