필터 지우기
필터 지우기

how to populate the left side with the same values as the right side in an array

조회 수: 3 (최근 30일)
Hello All,
I want to write a small code that, given an array with an n number of odd cells, populates the left side with the same values as the right side (already populated), and the center cell remains unchanged. I developed the following code:
for k=drange(1:n/2+0.5)
w(k)=input('please input half of the values')
end
p = randi(10,10,1); % Create Data
w = randi(10,10,1); % Create Data
[x L E I] = deal(3, 5, 7, 13); % Create Data
P(j)=-w(j)/(x*(15*x-37*L^2)*(76*H*Y))
q==0
for j=drange(n/2+1.5:n)
q=q+1
P(j)=P(n/2-0.5-q)
end
However, I keep obtaining different errors when I run this script. The most common is:
Index exceeds matrix dimensions.
Error in matlab_codigo (line 11) P(j)==P(n/2-0.5-q)
But other errors also occur. It depends of the inputs I give to the program.
Any help appreciated. Thanks
regards, Hugo

채택된 답변

Image Analyst
Image Analyst 2014년 12월 5일
편집: Image Analyst 2014년 12월 5일
Instead of doing things like n/2+0.5, use ceil(n/2). ceil() goes to the next higher integer. There is also a floor() function to go down (chop off the fraction), and a fix() which goes either way but always towards 0.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2014년 12월 5일
t = 1:floor(n/2);
P(:,t) = P(:,n+1-t);
  댓글 수: 3
Image Analyst
Image Analyst 2014년 12월 5일
What does drange() do? That's not in base MATLAB.
Roger Stafford
Roger Stafford 2014년 12월 5일
Try this, Hugo:
P = rand(7,5); % <-- Choose any sizes you please
n = size(P,2);
disp(P)
t = 1:floor(n/2);
P(:,t) = P(:,n+1-t);
disp(P)

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

카테고리

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