Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

How can I Read or get the Values one by one from matrix variable or array?

Asked by PRIYANGA

PRIYANGA (view profile)

on 13 Jul 2012
Accepted Answer by TAB

TAB (view profile)

Sir or Madam,

I want to read the array variable one by one from array or matrix? How can i write the code? I want to calculate the Zero Element and Non Zero Element in the matrix?

I have use s=nnz(x);

but i want to get the values one by one and the compare and calculate the zero and nonzero element.

thank you sir/madam.

0 Comments

PRIYANGA

PRIYANGA (view profile)

Products

No products are associated with this question.

2 Answers

Answer by TAB

TAB (view profile)

on 13 Jul 2012
Edited by TAB

TAB (view profile)

on 13 Jul 2012
Accepted answer
for x=1:length(YourMatrix)
  if YourMatrix(x)==0
    % Operation
  elseif  YourMatrix(x)~=0
    % Operation
  else
  end
end

But vectorization is always recommended in matlab programming instead of using loop whenever possible.

1 Comment

PRIYANGA

PRIYANGA (view profile)

on 13 Jul 2012

Ok sir thank you so much sir..

TAB

TAB (view profile)

Answer by Sebastian Holmqvist on 13 Jul 2012
z_elem = nnz(nnz == 0)
nz_elem = nnz(nnz ~= 0)

Then do your calculations on each.

1 Comment

PRIYANGA

PRIYANGA (view profile)

on 13 Jul 2012

Ok sir thank you so much..

Contact us