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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:56:23+00:00 2026-06-13T09:56:23+00:00

This post follows a previous question regarding the restructuring of a matrix: re-formatting a

  • 0

This post follows a previous question regarding the restructuring of a matrix:

re-formatting a matrix in matlab

An additional problem I face is demonstrated by the following example:

depth = [0:1:20]';
data = rand(1,length(depth))';
d = [depth,data];
d = [d;d(1:20,:);d];

Here I would like to alter this matrix so that each column represents a specific depth and each row represents time, so eventually I will have 3 rows (i.e. days) and 21 columns (i.e. measurement at each depth). However, we cannot reshape this because the number of measurements for a given day are not the same i.e. some are missing. This is known by:

dd = sortrows(d,1);
for i = 1:length(depth);
    e(i) = length(dd(dd(:,1)==depth(i),:));
end

From ‘e’ we find that the number of depth is different for different days. How could I insert a nan into the matrix so that each day has the same depth values? I could find the unique depths first by:

unique(d(:,1))
From this, if a depth (from unique) is missing for a given day I would like to insert the depth to the correct position and insert a nan into the respective location in the column of data. How can this be achieved?

  • 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-13T09:56:24+00:00Added an answer on June 13, 2026 at 9:56 am

    You were thinking correctly that unique may come in handy here. You also need the third output argument, which maps the unique depths onto the positions in the original d vector. have a look at this code – comments explain what I do

    % find unique depths and their mapping onto the d array
    [depths, ~, j] = unique(d(:,1));
    
    % find the start of every day of measurements
    % the assumption here is that the depths for each day are in increasing order 
    days_data = [1; diff(d(:,1))<0];
    
    % count the number of days
    ndays = sum(days_data);
    
    % map every entry in d to the correct day
    days_data = cumsum(days_data);
    
    % construct the output array full of nans
    dd = nan(numel(depths), ndays);
    
    % assing the existing measurements using linear indices
    % Where data does not exist, NaN will remain
    dd(sub2ind(size(dd), j, days_data)) = d(:,2)
    
    dd =
    
    0.5115    0.5115    0.5115
    0.8194    0.8194    0.8194
    0.5803    0.5803    0.5803
    0.9404    0.9404    0.9404
    0.3269    0.3269    0.3269
    0.8546    0.8546    0.8546
    0.7854    0.7854    0.7854
    0.8086    0.8086    0.8086
    0.5485    0.5485    0.5485
    0.0663    0.0663    0.0663
    0.8422    0.8422    0.8422
    0.7958    0.7958    0.7958
    0.1347    0.1347    0.1347
    0.8326    0.8326    0.8326
    0.3549    0.3549    0.3549
    0.9585    0.9585    0.9585
    0.1125    0.1125    0.1125
    0.8541    0.8541    0.8541
    0.9872    0.9872    0.9872
    0.2892    0.2892    0.2892
    0.4692       NaN    0.4692
    

    You may want to transpose the matrix.

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

Sidebar

Related Questions

This post if a follow-up question to mt previous post: Android RESTful Web application
This is a follow-up question related to my previous post . Below is a
That's a question really similar to this previous post of mine. I need to
This is a follow up question to a previous post on saving recent searches
I had the following error at previous question: Fluent NHibernate? Am I doing this
This is a follow-up question of my previous post . I am trying to
This is a follow up to my previous question about using XMLHttpRequest() to post
This is a follow up post of my previous question about BASIC auth over
This is a follow-up question to my previous post: Python/Scipy Interpolation (map_coordinates) Let's say
This post asks this question but doesn't really give an answer, so I thought

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.