Subscript indices must either be real positive integers or logicals???

Hi ,everyone i am pretty new to MATLAB i tried to sum up the elements in each row , but i keep getting the errors ..... any suggestion why ? thank you so much
clear all % Good practice to clear the work space at the beginning
length = 7;
numOfWords = 1000 ;
p = 0.1; %Probability of bit error in the BSC
codes = randi ([0 1],[numOfWords length]);
sum = zeros (numOfWords,1);
for i = 1 : length
sum= sum+ codes(:,i);
end
odd_ix = find(mod(sum, 2) == 1);
even_ix = find(mod(sum, 2) == 0);
sum(odd_ix) = 1 ;
sum(even_ix) = 0 ;
codes (:,length+1)=sum ;
output = bsc(codes, p); %Simulate the BSC
E = mod(codes+output, 2);
sum(E,2);

Answers (2)

l = 7; % length() is a builtin function; don't alias it...
numOfWords = 1000 ;
p = 0.1; %Probability of bit error in the BSC
codes = randi ([0 1],[numOfWords l]);
% sum = zeros (numOfWords,1); % sum is also builtin and an important one...
s=sum(codes,2); % sum over rows of codes array (see: doc sum)
First, please do not use sum as a variable name. It is a reserved word (and useful function, it returns the sum of its arguments), and using it as a variable will cause problems if you want to use it as a function later.
The problem (other that you are using sum as a variable name for an array) is that your codes array is an array of ones an zeros, as is your E array. The zeros in E are throwing the error when you call your (unfortunately named) sum(E,2).
I simply set output to equal 1 because I couldn’t find bsc as a function in your code. I don’t know what it does or what it should return.
Other than not using sum as a variable name (call it csum or some such) I suggest that a fix for it might be to use sum(E+1,2) if you want to use codes or E as array indices.

Categories

Tags

Asked:

on 18 May 2014

Answered:

on 18 May 2014

Community Treasure Hunt

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

Start Hunting!