error in using the sum function

조회 수: 18 (최근 30일)
Eliraz Nahum
Eliraz Nahum 2018년 9월 27일
편집: Adam Danz 2018년 9월 27일
I am getting an error I can't understand.
I attached the code and a photo.
thanks!
clear all
close all
clc
m=5; %number of groups
n=10 %size of each group
sz=[m n];
sum=0;
G=0+(100-0)*rand([sz]);
G1=G(1,:);
sum(G1);
  댓글 수: 1
jonas
jonas 2018년 9월 27일
편집: jonas 2018년 9월 27일
It can be quite useful to read the error message returned by MATLAB.
" 'sum' appears to be both a function and a variable. If this is unintentional, use 'clear sum' to remove the variable 'sum' from the workspace."

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

채택된 답변

Adam Danz
Adam Danz 2018년 9월 27일
편집: Adam Danz 2018년 9월 27일
You are defining a variable named sum here
sum=0;
and then later you try to index that variable by a vector of non-integers. It's like doing this:
v= 6;
v([310.15324, 235.3463, 501.346])
If you're trying to use the sum() function, you'll need to use a different variable name.

추가 답변 (1개)

ANKUR KUMAR
ANKUR KUMAR 2018년 9월 27일
You have already defined sum as a variable
clc
clear
m=5; %number of groups
n=10 %size of each group
sz=[m n];
G=0+(100-0)*rand([sz]);
G1=G(1,:);
sum(G1)
sum(G,1)%along first dimension
sum(G,2) %along second dimension

카테고리

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

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by