Removing an array dimension where the elements sum to zero
Posted
by James
on Stack Overflow
See other posts from Stack Overflow
or by James
Published on 2010-04-03T19:28:56Z
Indexed on
2010/04/03
19:33 UTC
Read the original article
Hit count: 321
Hi,
I am assigning a 3D array, which contains some information for a number of different loadcases. Each row in the array defines a particular loadcase (of which there are 3) and I would like to remove the loadcase (i.e. the row) if ALL the elements of the row (in 3D) are equal to zero.
The code I have at the moment is: Array = zeros(3,5) % Initialise array
Numloadcases = 3;
Array(:,:,1) = [10 10 10 10 10;
0 0 0 0 0;
0 0 0 0 0;]; % Expand to a 3D array
Array(:,:,2) = [10 10 10 10 10;
0 0 0 0 0;
0 0 0 0 0;];
Array(:,:,3) = [10 10 10 10 10;
0 0 0 0 0;
0 0 20 0 0;];
Array(:,:,4) = [10 10 10 10 10;
0 0 0 0 0;
0 0 20 0 0;];
% I.e. the second row should be removed.
for i = 1:Numloadcases
if sum(Array(i,:,:)) == 0;
Array(i,:,:) = []
end
end
At the moment, the for loop I have to remove the rows causes an indexing error, as the size of the array changes in the loop.
Can anyone see a work around for this?
Thanks
© Stack Overflow or respective owner