subs Command

조회 수: 18 (최근 30일)
Adam Anderson
Adam Anderson 2012년 2월 3일
편집: Andreas 2013년 9월 27일
Is it possible to make an array in a function then substitute a certain element to another element. I have a beam equation with certain forces on different parts. So I created a variable for the length and am given an expression for the whole beam. So I split the equation into multiple parts and now want to evaluate each piece through different lengths. Any help would be appreciated. Thanks

채택된 답변

Geoff
Geoff 2012년 2월 3일
From your description I gather that you want to swap out a range of values from an array into new indices within that array. You can modify values in a range by simply passing that range as a subscript on the left and passing new values (the same length) on the right...
Let's say I have the array:
x = 1:20
I can swap 5 elements from either end like this:
x([1:5 15:20]) = x([15:20 1:5])
Or duplicate sections
x(1:4) = x(5:8)
Or just mess it up
x([5 9 20 13:16]) = x([1 4 7 10 11 18 19 20])
  댓글 수: 1
Adam Anderson
Adam Anderson 2012년 2월 3일
Thanks for the input and I even tried it that way but because of the sizing I couldn't do it like that because I couldn't always break the elements up into what I needed. I did solve it though with the sign operator which allowed the program to auto size the elements needed in the array.

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

추가 답변 (1개)

Adam Anderson
Adam Anderson 2012년 2월 3일
function beam(x)
x1 = linspace(0,x);
n=length(x1);
for j=1:n
y(j) = -5/6.*(sign(x1(j),0,4)-sign(x1(j),5,4));
y(j) = y(j) + 15/6.*sign(x1(j),8,3) + 75*sign(x1(j),7,2);
y(j) = y(j) + 57/6.*x1(j)^3 - 238.25.*x1(j);
end
plot(x1,y)
This is what it turned out to be. I had to break the polynomial up into three separate equations.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by