Solved

Can someone please help with this simple Matlab question

Posted on 2009-04-02
1
61 Views
Last Modified: 2016-02-10
Question is;

Create a (10 by 10) matrix initialized to all zeros.
Input the numbers 1 to 100 using 2 nested for loops.

my attempt is in the codebox below

Can someone please advise?
Z=zeros(10,10);
count=0;
for x = 1:10
    for y = 1:10
        Z(x,y)=count+1;
    end
end
        Z

Open in new window

0
Comment
Question by:jtiernan2008
1 Comment
 
LVL 15

Accepted Solution

by:
yuk99 earned 250 total points
ID: 24052089
This will be very slow in Matlab.
Do this:
Z=reshape(1:100,10,10)';

1:100 creates vector and RESHAPE function reshapes it to square matrix. You have to transpose it with ' to have sequence by rows.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question