question about matlab code and function......

im trying to write a function on matlab which can randomly change one bit per column in matrix, for example like , if i have have a 4*4 matrix , i wanna the out put has one bit changed for each column.
for convenience i set up all my data 0 and 1. and basic idea of 'change' is flip 0 to 1 or 1 to 0.
and someone give me the code below, i can see how it works....but im not quite sure how to convert this code into a function. i was trying to do so but it always return error....
A = round(rand(4,4)); % The initial matrix.
% Given A, use this to change one random element per column.
[m,n] = size(A); % Just to be general...
idx = ceil(rand(1,n)*m)+(0:n-1)*m;
A(idx) = ~A(idx)
like i only have 4*4 matrix data input and call it notisify. but its not work...
function [A] = notisify(x)
if size(x) == [4,4];
idx = ceil(rand(1,4)*4)+(0:4-1)*4;
A(idx) = ~A(idx);
end
why is this not work?thanks for your help.......this stuff make me sick....
[EDITED, code formatted, Jan]

3 Comments

Jan
Jan on 4 Sep 2012
Edited: Jan on 4 Sep 2012
Please format your code by your own in the future.
"It does not work" and "it makes me sick" is not a helpful description of the problem. Please take the time to analyse what's going wrong. It is very likely, that such a detailed analysis reveals the solution already - nothing else happens, when the problem is discussed and solved in a forum. When you get an error message, post it completely. When the results differ from your expectations, explain both exhaustively.
Simon the answers forum editor is maby the problem, when I begin, I had some difficulties to find out how to use it. I think mathworkers should think about it
Please, Azzi, send this and any suggestions for improvements to files@mathworks.com . The frequent contributors, e.g. all editors and the admins, have lost their possibility to see through the eyes of a beginner. On the other hand, it is hard to encourage beginners to submit enhancement requests. Unfortunately the proper formatting, one of the main advantages of this forum compared to CSSM, seem to be the main cause of troubles and misunderstanding also.

Sign in to comment.

 Accepted Answer

Matt Fig
Matt Fig on 4 Sep 2012
Edited: Matt Fig on 4 Sep 2012
You might want to read the Getting Started section of the documentation. Also, why would you want the function to only work on 4-by-4 matrices? You can make it to work for a general matrix, then still only pass it 4-by-4 matrices! It is usually better to make your functions more general, then make the code that calls the function check that the data has the right size for your particular application.
This code will only work on 4-by-4 arrays, as you seem to want. If it were me, I would use the more general form you were given in a previous post...
function x = notisify(x)
% NOTISIFY changes one random element per column.
% B = notisify(A) where A is a 4x4 binary matrix,
% returns B such that B is identical to A except
% in one element per column.
if isequal(size(x),[4,4])
idx = ceil(rand(1,4)*4)+(0:4-1)*4;
x(idx) = ~x(idx);
else
error('NOTISIFY only works on 4x4 matrices.')
end
Here is a general function that will work on any random binary array. You can use it only on 4-by-4 matrices but it will work on m-by-n (or m-by-n-by-v, or whatever!).
function x = notisify(x)
% NOTISIFY changes one random element per column.
% B = notisify(A) where A is a binary array,
% returns B such that B is identical to A except
% in one element per column.
[m,n] = size(x);
idx = ceil(rand(1,n)*m)+(0:n-1)*m;
x(idx) = ~x(idx);

More Answers (2)

%try this
function A= notisify(x)
A=x;
if size(x)==[4,4]
idx = ceil(rand(1,4)*4)+(0:4-1)*4;
x(idx) = ~x(idx);
A=x;
end

2 Comments

What is the real difference to the original version? The original code is more efficient, and A replied without any changes and assignments, when the size does not match.
A is not an input argument;
A(idx) = ~A(idx);
it can't be calculated

Sign in to comment.

The == operator compare its arguments elementwise and replies a vector. Therefore if size(x)==[4,4] crashs, if x has more than 2 dimensions. Better:
if isequal(size(x), [4,4])

Categories

Community Treasure Hunt

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

Start Hunting!