How to have array in for loop

Hi,
So I have a matrix and I want to divide some columns by other columns in a for loop, because I want to divide sets of 3 columns by different sets, so i have created this example:
one=1:24;
two= 25:48;
three=49:72;
four=73:96;
M=[one;two;three;four]
for i=1:4
for j=1:3
Mnew(i,j)=M(i,j)./M(i,16:18);
end
for j=4:6
Mnew(i,j)=M(i,j)./M(i,19:21);
end
end
How can I make j=1:3 literally an array such as 16:18, because now it won't let me run this.

Answers (1)

Like so:
one=1:24;
two= 25:48;
three=49:72;
four=73:96;
M=[one;two;three;four];
for i=1:4
j = 1:3;
Mnew(i,j)=M(i,j)./M(i,16:18);
j = 4:6;
Mnew(i,j)=M(i,j)./M(i,19:21);
end

This question is closed.

Products

Release

R2019b

Asked:

on 23 Sep 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!