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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:25:01+00:00 2026-05-25T18:25:01+00:00

I have a problem in storing the data in matrix in for and if

  • 0

I have a problem in storing the data in matrix in for and if loops,

The results give me only the last value of the last iteration. I want all the

results of all iterations to be stored in a matrix be sequence.

Here is a sample of my code:

clear all

clc

%%%%%%%%%%%%%%

 for M=1:3;

    for D=1:5;

%%%%%%%%%%%%%%

    if ((M == 1) && (D <= 3)) || ((M == 3) && (2 <= D  && D <= 5))

        U1=[5 6];

    else

        U1=[0 0];

    end

    % desired output: 

    % U1=[5 6 5 6 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 5 6 5 6 5 6]

%%%%%%%%%%%%%%

    if (M == 1) && (D==4) || ((M == 3) && (D == 1))

        U2=[8 9];

    else

        U2=[0 0];

    end

    % desired output: 

    % U2=[0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0]

%%%%%%%%%%%%%%

    if ((M == 1) && (D == 5)) || ((M == 2) && (1 <= D  && D <= 5)) 

        U3=[2 6];

    else

        U3=[0 0];

    end

    % desired output:

    % U3=[0 0 0 0 0 0 0 0 2 6 2 6 2 6 2 6 2 6 2 6 0 0 0 0 0 0 0 0 0 0]

 %%%%%%%%%%%%%%

    end
end
  • 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-25T18:25:01+00:00Added an answer on May 25, 2026 at 6:25 pm

    You are overwriting your matrices each time you write UX=[X Y];.

    If you want to append data, either preallocate your matrices and specify the matrix index each time you assign a new value, or write UX=[UX X Y]; to directly append data at the end of your matrices.

    clear all
    clc
    U1=[];
    U2=[];
    U3=[];
    for M=1:3
        for D=1:5
            if ((M == 1) && (D <= 3)) || ((M == 3) && (2 <= D  && D <= 5))
                U1=[U1 5 6]; 
            else
                U1=[U1 0 0];
            end
            % desired output: 
            % U1=[5 6 5 6 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 6 5 6 5 6 5 6]
            if (M == 1) && (D==4) || ((M == 3) && (D == 1))
                U2=[U2 8 9];
            else
                U2=[U2 0 0];
            end
            % desired output: 
            % U2=[0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0 0 0 0 0 8 9 0 0 0 0 0 0 0 0]
            if ((M == 1) && (D == 5)) || ((M == 2) && (1 <= D  && D <= 5)) 
                U3=[U3 2 6];
            else
                U3=[U3 0 0];
            end
            % desired output:
            % U3=[0 0 0 0 0 0 0 0 2 6 2 6 2 6 2 6 2 6 2 6 0 0 0 0 0 0 0 0 0 0]
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem storing my inserted data to my database. When I'm using
I am using LaTeX and I have a problem concerning string manipulation. I want
I have a problem while converting a string whose value is dd.mm.yyyy to DateTime
i have a problem. i want to get info about my string have other
My main problem is storing the data which is not supported by Core Data.
I am facing a problem storing original values of a form. I have a
I am storing data in xml file, In one of the node I have
I think I have the code correct for storing data in an ArrayList. However,
Right now I have a Rails 3 model for storing profile data. One of
I have some hierarchy: base, derived classes and some structure storing user data as

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.