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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:42:53+00:00 2026-06-01T19:42:53+00:00

Am new to matlab. Can someone explain me the following code. this code is

  • 0

Am new to matlab.
Can someone explain me the following code. this code is used for training the neural network

N = xlsread('data.xls','Sheet1');
N = N(1:150,:);
UN = xlsread('data.xls','Sheet2');
UN = UN(1:150,:);
traindata = [N ; UN];
save('traindata.mat','traindata');
label = [];
for i = 1 : size(N,1)*2
if( i <= size(N,1))
%        label = [label ;sum(traindata(i,:))/size(traindata(i,:),2)];
     label = [label ;sum(traindata(i,:))/10];
else
%        label = [label ;sum(traindata(i,:))/size(traindata(i,:),2)];
     label = [label ;sum(traindata(i,:))/10];
end
end
weightMat = BpTrainingProcess(4,0.0001,0.1,0.9,15,[size(traindata,1) 1],traindata,label);
  • 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-01T19:42:54+00:00Added an answer on June 1, 2026 at 7:42 pm

    I cannot find a Neural Network toolbox built-in that corresponds to BpTrainingProcess(), so this must be a file you have access to locally (or you need to obtain from the person who gave you this code). It likely strings together several function calls to Neural Network toolbox functions, or perhaps is an original implementation of a back-propagation training method.

    Otherwise, the code has some drawbacks. For one, it doesn’t appear that the interior if-else statement actually does anything. Even the lines that are commented out would leave a totally useless if-else setup. It looks like the if-else is intended to let you do different label normalization for the data loaded from Sheet1 of the Excel file vs. data loaded from Sheet2. Maybe that is important for you, but it’s currently not happening in the program.

    Lastly, the code uses an empty array for label and the proceeds to append rows to the empty array. This is unneeded because you already know how many rows there will be (it will total up to size(N,1)*2 = 150*2 = 300 rows. You could just as easily set label=zeros(300,1) and then use usual indexing at each iteration of the for-loop: label(i) = .... This saves time and space, but arguably won’t matter much for a 300-row data set (assuming that the length of each row is not too large).

    I put documentation next to the code below.

    % The functionn 'xlsread()' reads data from an Excel file.
    % Here it is storing the values from Sheet 1 of the file 'data.xls'
    % into the variable N, and then using the syntax N = N(1:150,:) to
    % change N from being all of the data into being only the first
    % 150 rows of the data
    N = xlsread('data.xls','Sheet1');
    N = N(1:150,:);
    
    % Now do the same thing for Sheet 2 from the Excel file.
    UN = xlsread('data.xls','Sheet2');
    UN = UN(1:150,:);
    
    % This concatenates the two different data arrays together, making
    % one large array where N is the top half and UN is the bottom half.
    % This is basically just stacking N on top of UN into one array.
    traindata = [N ; UN];
    
    % This saves a copy of the newly stacked array into the Matlab data file
    % 'traindata.mat'. From now on, you should be able to load the data from
    % this file, without needing to read it from the Excel sheet above.
    save('traindata.mat','traindata');
    
    % This makes an empty array which will have new things appended to it below.
    label = [];
    
    % Because UN and N have the same number of rows, then the training data
    % has twice as many rows. So this sets up a for loop that will traverse
    % all of these rows of the training data. The 'size()' function can be
    % used to get the different dimensions of an array.
    for i = 1 : size(N,1)*2
    
        % Here, an if statement is used to check if the current row number, i,
        % is less than or equal to than the number of rows in N. This implies
        % that this part of the if-statement is only for handling the top half
        % of 'trainingdata', that is, the stuff coming from the variable N.
    
        if( i <= size(N,1))
           % The line below was already commented out. Maybe it had an old use
           % but is no longer needed?
           % label = [label ;sum(traindata(i,:))/size(traindata(i,:),2)];
    
           % This syntax will append new rows to the variable 'label', which
           % started out as an empty array. This is usually bad practice, memory-wise
           % and also for readability.
    
           % Here, the sum of the training data is being computed, and divided by 10
           % in every case, and then appended as a new row in 'label'. Hopefully,
           % if you are familiar with the data, you will know why the data in 'N'
           % always needs to be divided by 10.
           label = [label ;sum(traindata(i,:))/10];
    
        % Otherwise, if i > # of rows then handle the data differently.
        % Really this means the code below treats only data from the variable UN.
        else
           % The line below was already commented out. Maybe it had an old use
           % but is no longer needed?
           % label = [label ;sum(traindata(i,:))/size(traindata(i,:),2)];
    
           % Just like above, the data is being divided by 10. Given that there
           % is nothing different about the code here, and how it modifies 'label'
           % there is no need for the if-else statements, and they only waste time.
           label = [label ;sum(traindata(i,:))/10];
    
        % This is needed to show the end of the if-else block.
        end
    
    % This is needed to show the end of the for-loop.
    end
    
    
    % This appears to be a Back-Propagation Neural Network training function.
    % This doesn't match any built-in Matlab function I can find, but you might
    % check in the Neural Network toolbox to see if the local function
    % BpTrainingProcess is a wrapper for a collection of built-in training functions.
    
    weightMat = BpTrainingProcess(4, 0.0001, 0.1, 0.9, 15,
                                  [size(traindata,1) 1], traindata,label);
    

    Here is a link to an example Matlab Neural Network toolbox function for back-propagation training. You might want to look around the documentation there to see if any of it resembles the interior of BpTrainingProcess().

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

Sidebar

Related Questions

I am new to MATLAB, and I can't fathom this from the documentation. function
I'm a beginner in MATLAB and hope someone can help me with this problem.
I guess this is something from new version of Matlab. But I can't figure
Hello I am new to MATLAB , I wanted to know how can I
Apologies if this is a stupid question, I'm relatively new to Matlab. I've got
Would somebody help me please in this question. I'm new in Matlab... And it's
I'm sorry if this is a stupid question, but I'm very new to Matlab,
I am wondering whether I can do the following efficiently in Matlab. Writing a
I am new to MATLAB, i wanted to know that can i extract a
I am new to Octave/Matlab so so far I know, you can apply matrix

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.