Creating a matrix using arrays

1 view (last 30 days)
Anna M
Anna M on 9 Mar 2020
Commented: Anna M on 9 Mar 2020
I am trying to create a matrix where each row is xstart:inc:xend, where the value begins at xstart and is increased by xinc to xend. so, a 30 x 37 matrix.
I am using the following:
XL = 96.5;
dx = 0.423;
n = 1:30;
xstart = 0+n*dx;
xend = XL-n*dx;
xinc = xend/37;
what I have so far,
xr = zeros(30,37);
for i = 1:length(n)
xr(i,:) = xstart(1,i):xinc(1,i):xend(1,i);
end
I have tried different variations of the left side, xr(i), but I am still getting an error that states, "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
I computed the following, but I was wondering if I have to do this 30 times.
xr1 = xstart(1,1):xinc(1,1):xend(1,1);
xr2 = xstart(1,2):xinc(1,2):xend(1,2);
xr3 = xstart(1,3):xinc(1,3):xend(1,3);

Accepted Answer

Akira Agata
Akira Agata on 9 Mar 2020
Edited: Akira Agata on 9 Mar 2020
How about the following?
XL = 96.5;
dx = 0.423;
n = 1:30;
xstart = 0+n*dx;
xend = XL-n*dx;
xr = zeros(30,37);
for kk = 1:length(n)
xr(kk,:) = linspace(xstart(kk),xend(kk),37);
end
or
xr = arrayfun(@(x,y) linspace(x,y,37),xstart',xend','UniformOutput',false);
xr = cell2mat(xr);
  1 Comment
Anna M
Anna M on 9 Mar 2020
Both work for what I need! Thank you very much!

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!