how do i get rid of the fractions on array calculations

조회 수: 6 (최근 30일)
Anantha Padmanabhan
Anantha Padmanabhan 2014년 9월 30일
편집: Roger Stafford 2014년 10월 1일
for X=1:0.1:64;
for Y=1:0.1:64
vble(X,Y)=-0.5*sin(X*2*3.14/64*sin(Y*2*3.14/64);
if vble(X,Y)==psisecondsecond1;
vble(X,Y)=psisecondsecond1;
else
vble=0;
end
end
end
I want to know how i can store the values of vble that are calculated for the fractional values of X and Y. Like how do i store vble(1.1,5.5). i know matlab cant store fractions but i am not able to write out a for loop for my required needs. Thanks, Ananth

답변 (2개)

Roger Stafford
Roger Stafford 2014년 10월 1일
편집: Roger Stafford 2014년 10월 1일
You need to proceed differently in your for loops.
n = 631;
for ix = 1:n
X = .1*(ix-1)+1;
for iy = 1:n
Y = .1*(iy-1)+1;
vble(ix,iy)=-0.5*sin(X*2*3.14/64*sin(Y*2*3.14/64);
% (Correction corrected)
....
Another point to make here is that you are demanding exact equality when you write
vble(X,Y)==psisecondsecond1
but since your computation involves the sine function, round-off errors may convert exact equalities into close approximations. Instead you should write
abs(vble(X,Y)-psisecondsecond1)<tol
for some very small value 'tol'.
  댓글 수: 2
the cyclist
the cyclist 2014년 10월 1일
Roger, is your correction correct? I see you are using both X(ix) and Y*(iy). I did not dig into the details, but I'm just guessing from symmetry that one of those is not what you intended.
Roger Stafford
Roger Stafford 2014년 10월 1일
You are right Cyclist. The way I had it originally was correct, so I have corrected the correction.

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


the cyclist
the cyclist 2014년 10월 1일
I typically do this as follows:
X=1:0.1:64;
numberOfX = numel(X);
Y=1:0.1:64;
numberOfY = numel(Y);
for nx = 1:numberOfX
for ny = 1:numberOfY
vble(nx,ny) = ...
end
end
Inside the for loop, you'll need to reference X and Y by indices:
X(nx)
Y(ny)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by