question of applying sum() to the matrix with zero values

조회 수: 4 (최근 30일)
my matrix is X=[1 0 0 ;0 0 0;0 9 0](actual matrix is larger than this and having less values more zoros) my question is ,when applying sum(X),it gives error message "??? Subscript indices must either be real positive integers or logicals."

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 7일
You've probably used sum as a variable in your code. Try this
clear sum
X=[1 0 0 ;0 0 0;0 9 0]
sum(X)
  댓글 수: 3
Image Analyst
Image Analyst 2013년 9월 7일
Be aware that sum will be over columns and will be a ax3 array. If you want the sum of the whole array, you can do this:
theSum = sum(X(:));
which turns X into a 1D vector before it gets summed. theSum will then be a scalar (single number) rather than a 1 by 3 array.
mahesh chathuranga
mahesh chathuranga 2013년 9월 9일
thank you very much for helping me.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by