How to increment a for loop through list elements in a vector, output a matrix of the same dimensions?

1 view (last 30 days)
I have a vector s = [a b c d], each of the elements in the vector are 7 by 1 lists of 1's and 0's.
I need to make a for loop that will use the randerr function to flip 1 bit in each list. This is what I have so far:
for i = 1:length(s);
t = randerr(1,7);
reshape(t,7,1);
u = abs(s(:,i)-t);
end
But Matlab tells me the matrix dimensions do not agree..
I am obviously doing something wrong when trying to apply -t to each of the elements of s, but I've been searching through these forums and google for a while now and I can't find anything helpful.
Any help would be greatly appreciated.

Accepted Answer

Stephen23
Stephen23 on 10 Sep 2014
Edited: Stephen23 on 10 Sep 2014
MATLAB does not have a data type "list". I presume you are talking about numeric data, ie a numeric array. What is referred to in some programming languages as a "list" is called a cell array in MATLAB.
You are subtracting t (size 1x7) from s(:,i) (size 7x1). These dimensions do not match, as the error message clearly states. Did you check the dimensions using the size command?
Presumably you intended that reshape command to change the shape of t , but without allocating its output to a variable, it does nothing whatsoever in this code. Instead of using reshape , why not just use randerr to create t with the correct size?
  1 Comment
Callan Turley
Callan Turley on 18 Sep 2014
Thanks Stephan, I realize what I was doing wrong. I didn't realize there was a way to make randerr output a 7 x 1 array. I had a look around but I must have missed it.
Anyway, thanks for your help.

Sign in to comment.

More Answers (0)

Categories

Find more on Communications Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!