×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Help regarding Use of basic loop in matlab

Help regarding Use of basic loop in matlab

Help regarding Use of basic loop in matlab

(OP)
Hi everybody,

At present, I am learning to work with matlab. I want to learn how to implement a basic loop for the following steps I perform.
I have an excel file named "book" in format (.xlsx). I have two columns with ten rows each as shown below :

CODE

serial  value  
1        0.15
2        0.20
3        0.52
4        0.24
5        0.05
6       -2.05 

What I want to do ?

(i) Read the excel file in xlsx into matlab
(ii) For first 3 values in Column_1 , multiply with value 2 and Assign to a new array. Lets say array as "a"
(iii)For last 3 values in column_1 , multiply with value 3 and Assign to a new array. Lets say array as "b"
(iv) Concatenate the new arrays "a" and "b" into a single array . Lets say array as "c"
(v) Assign a new array for column_2 of the excel file, assign an array "d"
(vi) plot array "c" vs "d"

What I did ?

CODE -->

data=xlsread('book');
a=data(1:3,1)*2;
b=data(4:6,1)*3;
c=[a;b];
d=data(:,2);
plot(c,d) 

What I wish to do ?

How to implement a loop to automate this process instead of writing separate syntax as I have did above . How to get to the same result with a use of loop in matlab ?

Kindly cast your opinion to optimise the process.

Thanks from Basic Matlab Learner

RE: Help regarding Use of basic loop in matlab

(OP)
Additional information :

Since, I work with large number of values (row wise) in Excel sheet in a single column , I want to multiply every 10 values in column1 with a factor , then next 10 values in column1 with an another factor And SO ON . I want to do this in a loop.

RE: Help regarding Use of basic loop in matlab

While a loop would certainly help, I read somewhere that if you're using a loop in Matlab you're probably doing it wrong. I thus offer this non-loop suggestion:

CODE --> Matlab

factors = [2 3]; % specify your multiplicative factors
every = 10; % specify the number of rows to apply each factor to

factors = repmat(factors, 1, every)'(:); % repeat, transpose and serialise the factors to form a column vector of factors

c = data(:,1).*factors
d = data(:,2)
plot(c,d) 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources