Effacer les filtres
Effacer les filtres

How can we write the for loop for accessing a table data?

2 vues (au cours des 30 derniers jours)
kelvina
kelvina le 28 Jan 2014
Modifié(e) : kelvina le 28 Jan 2014
i want to perform operations on a table data, for that i need to access data of table using its index and again updated value of that index data should be stored at the same position.
  • * For Example: * * A table having 3 columns and 10 rows, i have written following code but its not working,
for i=1:10
for j=1:3
if j==1
x=x<=5
else if j==2
x=x<=15
else
x=x<=25
end
end
end
Please correct my code......
  2 commentaires
Amit
Amit le 28 Jan 2014
You know x<=5 will create a logical vector for 0 where the conditions dont satisfy and 1 for where it will.
Amit
Amit le 28 Jan 2014
Moreover, by x<=5, are you trying to createe a logical vector for the whole matrix or just that column?

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Jan 2014
for i=1:10
for j=1:3
if j==1
x(i,j) = x(i,j) <= 5
else if j==2
x(i,j) = x(i,j) <= 15
else
x(i,j) = x(i,j) <= 25
end
end
end
  1 commentaire
kelvina
kelvina le 28 Jan 2014
Modifié(e) : kelvina le 28 Jan 2014
thanx, its working

Connectez-vous pour commenter.

Plus de réponses (1)

Amit
Amit le 28 Jan 2014
If Walter's interpretation is correct, then you do not need so many loops. A single loop approach will be:
A = [5 15 25];
for j = 1:3
x(:,j) = x(:,j) <= A(j);
end
or no loop way:
x = x <= repmat([5 15 25],10,1);

Catégories

En savoir plus sur Logical 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