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 4112784
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:14:23+00:00 2026-05-20T22:14:23+00:00

I’m pretty new at Matlab so would need a baby-steps explanation. I have some

  • 0

I’m pretty new at Matlab so would need a baby-steps explanation.

I have some MIDI data which looks a bit like this:

time on/off note
10 1 61
90 0 61
90 1 72
92 1 87
100 0 72

What I want to do is expand or ‘fill-in’ the gaps so that I have a row for every single moment in time, and I have columns which show which notes are on (there is often more than one note at the same time).

the ultimate goal is to do some calculations about the overall relationship between notes on (the harmonic dissonance) at a given time.

So I was thinking that maybe I needed a new column for every single possible note (there are 127), and then a 1 or 0 for every time. Or maybe I can just have a matrix which just tells me which notes are on (so the number of columns varies).

I wrote my own pseudo-code, but have no idea how to implement it. I suspect there is a simple function that can do this. Here is my pseudo-code:

start with 0, at time 0 in a new ‘notes-on matrix’
for numbers: 0 to n
if the number matches a number in the time column, go to the on/off column for that row.
if 1 in on/off column then copy number in notes column to ‘notes-on matrix’ for corresponding row
if 0 then don’t copy/do nothing.

if number doesn’t match number in time column
copy the previous row (which can be blank if there were no notes on).

for each row in the new ‘notes-on matrix’, arrange numbers low to high in different columns.

So can anyone tell me what to do?? I’m banging my head against a brick wall here!

  • 1 1 Answer
  • 3 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-20T22:14:24+00:00Added an answer on May 20, 2026 at 10:14 pm

    Here’s a solution that will work even if the list is in a completely random order. It is based on the following idea: The cumulative sum of the vector [0 1 0 0 -1 0 0] is [0 1 1 1 0 0 0]. This corresponds to “on” in time 2, and “off” in time 5. Now all we need to do is populate an array with 1 and -1, and run CUMSUM to transform it into an array that has, in each column, ones whenever the sound is on.

    I assume that there are 128 notes (0-127), and that you want to have one time step of silence (if all notes eventually end) at the very end. Note that Matlab starts counting at 1, so time 0 corresponds to row #1.

    %# midiData is a n-by-3 array with [time,on/off,note]
    midiData = [...
    10 1 61
    90 0 61
    90 1 72
    92 1 87
    100 0 72];
    
    %# do not call unique here, because repeated input rows are relevant
    
    %# note values can be from 0 to 127
    nNotes = 128;
    
    %# nTimepoints: find the highest entry in midiData's time-column
    %# add 2, because midiData starts counting time at 0
    %# and since we want to have one additional timepoint in the end
    nTimepoints = max(midiData(:,1))+2; 
    
    
    
    
    %# -- new solution ---
    %# because the input is a bit messed up, we have to use a more complicated
    %# solution. We'll use `accumarray`, with which we sum up all the
    %# entries for on (+1) and off (-1) for each row(time)/column(note) pair.
    %# after that, we'll apply cumsum
    
    %# transform the input, so that 'off' is -1
    %# wherever the second col of midiData is 0
    %# replace it with -1
    midiData(midiData(:,2)==0,2) = -1;
    
    %# create output in one step
    %# for every same coordinate (time,note), sum all the 
    %# on/offs (@sum). Force the output to be 
    %# a nTimepoints-by-nNotes array, and fill in zeros
    %# where there's no information
    output = accumarray(midiData(:,[1 3])+1,midiData(:,2),...
        [nTimepoints,nNotes],@sum,0);
    
    %# cumsum, and we're done
    output = cumsum(output,1);
    

    The previous solution, for completeness:

    %# --- old solution ---
    
     %# create output array, which we'll first populate with 1 and -1
    %# after which we transform it into an on-off array
    %# rows are timepoints, columns are notes
    output = zeros(nTimepoints,nNotes);
    
    %# find all on's 
    %# onIdx is 1 if the second column of midiData is 1
    onIdx = midiData(:,2) == 1;
    
    %# convert time,note pairs into linear indices for
    %# writing into output in one step
    %# Add 1 to time and note, respectively, so that we start counting at 1
    plusOneIdx = sub2ind([nTimepoints,nNotes],midiData(onIdx,1)+1,midiData(onIdx,3)+1);
    
    %# write "1" wherever a note turns on
    output(plusOneIdx) = 1;
    
    %# now do the same for -1
    offIdx = midiData(:,2) == 0;
    minusOneIdx = sub2ind([nTimepoints,nNotes],midiData(offIdx,1)+1,midiData(offIdx,3)+1);
    
    %# instead of overwrite the value in output, subtract 1
    %# so that time/note that are both on and off become zeros
    output(minusOneIdx) = output(minusOneIdx) - 1;
    
    %# run cumsum on the array to transform the +1/-1 into stretches of 1 and 0
    %# the 'dim' argument is 1, because we want to sum in the direction in 
    %# which rows are counted
    output = cumsum(output,1);
    
    %# for fun, visualize the result
    %# there's white whenever a note is on
    imshow(output)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I have an autohotkey script which looks up a word in a bilingual dictionary
I have just tried to save a simple *.rtf file with some websites and
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.