how do i get rid of the fractions on array calculations

6 vues (au cours des 30 derniers jours)
Anantha Padmanabhan
Anantha Padmanabhan le 30 Sep 2014
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

Réponses (2)

Roger Stafford
Roger Stafford le 1 Oct 2014
Modifié(e) : Roger Stafford le 1 Oct 2014
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 commentaires
the cyclist
the cyclist le 1 Oct 2014
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 le 1 Oct 2014
You are right Cyclist. The way I had it originally was correct, so I have corrected the correction.

Connectez-vous pour commenter.


the cyclist
the cyclist le 1 Oct 2014
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)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by