question about matlab code and function......
Show older comments
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
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.
Azzi Abdelmalek
on 4 Sep 2012
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
Jan
on 4 Sep 2012
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.
Accepted Answer
More Answers (2)
Azzi Abdelmalek
on 4 Sep 2012
Edited: Azzi Abdelmalek
on 4 Sep 2012
%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
Jan
on 4 Sep 2012
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.
Azzi Abdelmalek
on 4 Sep 2012
A is not an input argument;
A(idx) = ~A(idx);
it can't be calculated
Jan
on 4 Sep 2012
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
Find more on Startup and Shutdown 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!