필터 지우기
필터 지우기

Creating a matrix using arrays

조회 수: 4 (최근 30일)
Anna M
Anna M 2020년 3월 9일
댓글: Anna M 2020년 3월 9일
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);

채택된 답변

Akira Agata
Akira Agata 2020년 3월 9일
편집: Akira Agata 2020년 3월 9일
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
Anna M
Anna M 2020년 3월 9일
Both work for what I need! Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by