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

Jan
Jan 2012년 9월 4일
편집: Jan 2012년 9월 4일
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
Azzi Abdelmalek 2012년 9월 4일
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
Jan 2012년 9월 4일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt Fig
Matt Fig 2012년 9월 4일
편집: Matt Fig 2012년 9월 4일

0 개 추천

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);

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 4일
편집: Azzi Abdelmalek 2012년 9월 4일

0 개 추천

%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

Jan
Jan 2012년 9월 4일
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

댓글을 달려면 로그인하십시오.

Jan
Jan 2012년 9월 4일

0 개 추천

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])

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

질문:

2012년 9월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by