In a cell array, do all the cells in a row contain 0s?
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
bah327hp bah327hp
le 7 Juil 2017
Commenté : bah327hp bah327hp
le 11 Juil 2017
I am working with a cell array. If there are any rows with the same value in column 4, I want to delete one of them. What I am thinking I could do is to replace the row with the repeated value in column 4 with all 0s. Then, I could write only rows that do NOT contain only 0s to a new table. Here is the code that replaces a row with 0s. It works.
[~, ~, Matrix]=xlsread('TestEachSceneOnce.xls', 'Sheet1')
for m = 1:size(Matrix,1)
for n=1:size(Matrix,1)
if strcmp(Matrix(m,4),Matrix(n,4)) & Matrix{n,5}~='alreadydone' & m~=n % without m~=n, we would be checking if a cell is equal to itself
Matrix{n,5}='alreadydone'
for p = 1:size(Matrix,2) % number of rows
Matrix{n,p}=0
end
else
end
end
end
xlswrite('TestEachSceneOnce.xls', Matrix, 'Sheet2')
I am having trouble with this next part. When I try this:
t=readtable('TestEachSceneOnce.xls', 'Sheet', 'Sheet2', 'ReadVariableNames', false)
for q = 1:size(Matrix,1)
RowsOfZeros=(Matrix{q,1}==0)
badrows=all(RowsOfZeros,2)
t=t(~badrows,:)
end
writetable(t, 'TestEachSceneOnce.xls', 'Sheet', 'Sheet3', 'WriteVariableNames', false)
I get this error:
Row index exceeds table dimensions.
When I try this
t=readtable('TestEachSceneOnce.xls', 'Sheet', 'Sheet2', 'ReadVariableNames', false)
RowsOfZeros=all(Matrix,2)
badrows=all(RowsOfZeros,2)
t=t(~badrows,:)
writetable(t, 'TestEachSceneOnce.xls', 'Sheet', 'Sheet3', 'WriteVariableNames', false)
I get this error:
Undefined function 'all' for input arguments of type 'cell'.
Is there any way to say "if all the cells in a given row of Matrix contain 0, don't include them in t"?
The spreadsheet I am working with is attached.
2 commentaires
Guillaume
le 7 Juil 2017
Is there any way to say "if all the cells in a given row of Matrix contain 0, don't include them in t"? Of course, there is. You'll have to use cellfun or an explicit loop.
The more important question is why are you importing the same data twice, once as a cell array, once as a (lot easier to work with) table. Why aren't you using just the table.
With regards to your first error, You're getting the number of rows, iterate from one to that number of rows then proceed to delete some of the rows in the loop. Obviously since your table now has less row than you started with and you still go up to the original number of rows, you're going to get an error that your index is exceeds the number of rows in your now shortened table.
Note that your double for loop for the comparison is a waste of time. You could do the comparison in just one operation by vectorisation. However, before telling you how to do that, I think we need to understand this dual cell array/table import.
Réponse acceptée
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Tables 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!