got an error message
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
i wrote the following code:
a.name=[];
a.age=[];
a.sex=[];
a.ID=[];
a.GPA=[];
a.name={'bahar';'maryam';'ronia';'kaihan';'vahid';'werya';'hana'};
a.age={2;11;21;18;37;38;25}
a.sex={'f'; 'f'; 'f'; 'f'; 'm'; 'm'; 'm'}
a.ID={1234;11344;11344;98764;34786;12984;45295}
a.GPA={11;12;14;17;19;20;13}
for i=1:7
if a(i).ID==a(i+1).ID
a(i+1).ID=[];
end
end
but i have got this error message:
Index exceeds matrix dimensions.
Error in student11 (line 12)
if a(i).ID==a(i+1).ID
I dont know why? would you please help me?
2 commentaires
Ganesh Hegade
le 2 Août 2017
You can use like this. But if you want to delete the duplicate ID's after each loop then below code will not work.
for i=1:6
if a.ID{i} == a.ID{i+1}
a.ID{i+1}=[];
end
end
Réponse acceptée
Sanjay Nair
le 2 Août 2017
Hello Bahar,
I believe the issue here is that you have declared the variable "a" as a scalar struct that contains multiple cell-array variables. From the way you have written your for loop, it appears that what you really want is to do an array of structs containing multiple scalar variables.
As an example, I think you are trying to set up "a" so that the first element is defined as such:
a(1).name = 'bahar';
a(1).age = 2;
a(1).sex = 'f';
a(1).ID = 1234;
a(1).GPA = 11;
You may also find the following documentation from Mathworks helpful: https://www.mathworks.com/help/matlab/matlab_prog/ways-to-organize-data-in-structure-arrays.html
Plus de réponses (1)
Steven Lord
le 2 Août 2017
99 (or let's go with 9 to save some time) bottles of beer on the wall. 9 bottles of beer.
bottlesOfBeerOnTheWall = 1:9
Take one down, pass it around, 8 bottles of beer on the wall.
8 bottles of beer on the wall, 8 bottles of beer. Take one down, pass it around, 7 bottles of beer on the wall.
etc.
for k = 1:9
bottlesOfBeerOnTheWall(k) = []
end
We receive an error when we try to remove the sixth bottle. Note that we have not removed bottles 1, 2, 3, 4, and 5 at that point but have removed bottles 1, 3 (the new second bottle, once bottle 1 was removed), 5 (the new third bottle after 1 and 3 are gone), 7 (the new fourth bottle), and 9 (the new fifth bottle.)
If you're iterating through an array and deleting elements, there are a couple alternatives you can use to avoid this problem.
- Don't delete as you go. Create a vector (of linear or logical indices) of elements to be deleted then delete them all after you've finished iterating through the whole array.
bottlesOfBeer = [1 2 3 3 4 5 5 6];
shouldBeDeleted = false(size(bottlesOfBeer));
for k = 2:length(bottlesOfBeer)
if bottlesOfBeer(k-1) == bottlesOfBeer(k)
shouldBeDeleted(k) = true;
end
end
bottlesOfBeer(shouldBeDeleted) = []
- Start at the end and work your way forward.
bottlesOfBeerOnTheWall = 1:9
for k = 9:-1:1
bottlesOfBeerOnTheWall(k) = []
end
- Use an approach that avoids iterating. In this case, since you want to compare the diff-erence between adjacent elements, use diff.
bottlesOfBeer = [1 2 3 3 4 5 5 6];
sameAsPrevious = [false diff(bottlesOfBeer)==0]
bottlesOfBeer(sameAsPrevious) = []
1 commentaire
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!