how not to be read the cells with zero values.

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)

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

Thank you. but imagine that A is 3D matrix(with 4 in the third dimension) and some cells are zero in all its dimensions. in this situation I changed your code to something like that:
for column=1:columns
for row=1:row
if A(row,column,1)~=0 & A(row,column,2)~=0 & A(row,column,3)~=0 & A(row,column,4)~=0
% do something
end
end
end
but it was incorrect.
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.
no this code remove all cells with zero value. these cells may be zero only in dimension one for example. but I want remove cells that are common in all dimensions with zero value. for example imagine these simple matrices
d1=[1 2 3 4 0;2 3 4 6 0;2 3 4 0 0]
d2=[0 0 4 5 0;2 3 4 5 1;1 2 3 0 1]
r1=[4 4 4 4 4;4 4 4 4 4;4 4 4 4 4]
r2=[2 2 2 2 2;2 2 2 2 2;2 2 2 2 2]
d3=[1 2 3 4 0;2 3 4 6 2;2 3 4 0 3]
d4=[1 2 4 5 0;2 3 4 5 1;1 2 3 0 1]
create three 3D matrices like that:
d=cat(3,d1,d2)
dd=cat(3,d3,d4)
r=cat(3,r1,r2)
then run below simple code:
for i=1:3
for j=1:3
if all(d(i,j,:)~=0
s=d.*r
end
end
end
and also
for i=1:3
for j=1:5
if all(dd(i,j,:)~=0
t=dd.*r
end
end
end
if (all matrix(i,j,:)~=0) removes only common cells with zero value. the number of s and t that is repeated is equal in two sample code. but they are not equal.
I hope I can explain that.
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.
Thank you for your patient. i know that matrix is rectangular and I can't remove some elements. Actually I don't want remove special elements but I want to decrease these repeats. I Think if I can decrease them by eliminating zero values that are common in all dimensions I can do this. I'm sorry. Thank you.

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Tags

No tags entered yet.

Asked:

on 21 Nov 2015

Edited:

on 22 Nov 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!