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

1 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2014-9-10
编辑:Stephen23 2014-9-10
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 个评论
Callan Turley
Callan Turley 2014-9-18
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Communications Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by