How to solve "Matrix index is out of range for deletion." error?

67 vues (au cours des 30 derniers jours)
Panida Kaewniam
Panida Kaewniam le 17 Fév 2021
Could anyone tell me how to solve this error? I attach the code as below. Thanks in advance
Error is .....
Matrix index is out of range for deletion.
Error in ModeShape (line 12)
K(:,[(6*(i-1))+1:(6*(i-1))+6])=[]
K=load('Job-1-1_STIF2.mtx') % The mtx. file has 3 columns with plenty of rows
for i=1:1:10
K([(6*(i-1))+1:(6*(i-1))+6],:)=[]
K(:,[(6*(i-1))+1:(6*(i-1))+6])=[]
end
  1 commentaire
Rik
Rik le 17 Fév 2021
Are you sure you didn't make any mistake when writing the code that creates the indices? Did you try storing them in a separate variable to investigate the values?

Connectez-vous pour commenter.

Réponses (2)

Rik
Rik le 17 Fév 2021
Take a look at the indices on the second line:
i=1;
ind=(6*(i-1))+1:(6*(i-1))+6
ind = 1×6
1 2 3 4 5 6
Since you use them to index the columns (the second dimension) and you have only 3, this deletion will fail.
  2 commentaires
Panida Kaewniam
Panida Kaewniam le 18 Fév 2021
Thank you for your comment. I maybe didn't explain clearly. And I'm quite new in this area so I don't understand your answer. It will be grateful if you could clearify.
I want to plot mode shapes (eigenvector) of a plate by using MATLAB. I have got stiffness and mass matrices from ABAQUS (in mtx. files).
K=load('Job-1-1_STIF2.mtx'); % K=Stiffness matrix
M=load('Job-1_MASS2.mtx') ; % M=Mass matrix
disp('Enter number of nodes: ')
Node=input('Enter number of nodes: ');
for i=1:1:Node
K([(6*(i-1))+1:(6*(i-1))+6],:)=[];
K(:,[(6*(i-1))+1:(6*(i-1))+6])=[];
M([(6*(i-1))+1:(6*(i-1))+6],:)=[];
M(:,[(6*(i-1))+1:(6*(i-1))+6])=[];
end
[phi, w2]=eig(K,M);
D=diag(w2);
* Remark: Both mtx. files, there are 3 columns (1st row No./ 2nd Column No./ 3rd value). There are a lot of rows as attachment.
Rik
Rik le 18 Fév 2021
Aha, so you don't actually have your actual matrix. How is Matlab supposed to know that your first column is the row index and the second column is the column index?
You can probably use accumarray to convert your data to the actual matrix, after which you can use the code you posted.

Connectez-vous pour commenter.


Steven Lord
Steven Lord le 18 Fév 2021
Suppose you were a pirate being forced to wash the plank that your pirate crew uses to force people to walk the plank. The plank is six paces long, so you clean then step forward. But what you don't realize is that the plank has rotted a bit, so as soon as you step onto it the last pace of the board falls off. So you wash, step forward, wash, step forward, and repeat this process. You wash the fifth pace of the plank and go to take a step forward ... but that sixth pace of plank is no longer there!
That's effectively what you're doing here. I've wrapped this example in try / catch so I can run both this example (which won't work) and the next (which will) in the same answer.
try
plank = 1:6;
for k = 1:length(plank)
fprintf("Washed element " + plank(k) + newline)
fprintf("Element " + (7-k) + " fell off!" + newline)
plank(7-k) = []
end
catch theError
disp("The operation threw the following error: " + newline + theError.message)
end
Washed element 1
Element 6 fell off!
plank = 1×5
1 2 3 4 5
Washed element 2
Element 5 fell off!
plank = 1×4
1 2 3 4
Washed element 3
Element 4 fell off!
plank = 1×3
1 2 3
The operation threw the following error: Index exceeds the number of array elements (3).
In this case the plank starts off with six elements, then five, then four, and finally when you try to wash element four the plank only has three elements.
You could resolve this by marking in a separate array which elements get deleted at each step and deleting them all at once after the loop is complete.
values = 2:10;
toDelete = false(size(values));
for ind = 1:numel(values)
if isprime(values(ind))
fprintf("Element %d (%d) is prime. Marking for deletion." + newline, ind, values(ind))
toDelete(ind) = true;
end
end
Element 1 (2) is prime. Marking for deletion. Element 2 (3) is prime. Marking for deletion. Element 4 (5) is prime. Marking for deletion. Element 6 (7) is prime. Marking for deletion.
values(toDelete) = []
values = 1×5
4 6 8 9 10

Catégories

En savoir plus sur Resizing and Reshaping Matrices 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