how not to be read the cells with zero values.
Show older comments
Hi I have matrix A. This matrix has 1000 rows and 1000 column. many cells of this matrix have 0 as their values.I have to enter this matrix into a loop. this loop starts with:
for i=1:1000
for j=1:1000
....
.....
end
I want in this movement cells that have zero value not to be read. but when I write for i=1:1000 and for j=1:1000 these cells will be read then will be rejected. I hope I can explain my question.
Thank you
Answers (1)
Image Analyst
on 21 Nov 2015
For example
[rows, columns] = size(A);
for column = 1 : columns
for row = 1 : rows
if A(row, column) ~= 0
% Do something ONLY if the value is not zero.
end
end
end
There are vectorized ways to do things but it depends on exactly what you want to do if the value is non-zero.
5 Comments
fatema saba
on 22 Nov 2015
Edited: fatema saba
on 22 Nov 2015
Image Analyst
on 22 Nov 2015
Try
if all(A(row, column, :)
all(A(row, column, :) will give a vertical vector along the third dimension, and all() checks if all of those elements in that vector are non-zero.
fatema saba
on 22 Nov 2015
Edited: fatema saba
on 22 Nov 2015
Image Analyst
on 22 Nov 2015
First, let's start using correct terminology. They are not "cells" like Excel calls them - they're "elements". "Cells" are a different beast entirely and I very much recommend you learn about them from the FAQ: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
What do you want to do? Do you want to compact the array be squeezing it together to get rid of ows and columns from a 3D array where all the elements along the third dimension? You can't arbitrarily remove rows and columns from a 3D array where all the elements along the third dimension. This is because the array must remain rectangular. For example if you were to remove 3 columns from row 1 and 8 columns from row 2, now row 1 and row 2 would not have the same number of columns in them and your array is no longer rectangular - it has a ragged right edge.
fatema saba
on 22 Nov 2015
Edited: fatema saba
on 22 Nov 2015
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!