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 个评论
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
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
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.
采纳的回答
更多回答(2 个)
Azzi Abdelmalek
2012-9-4
编辑:Azzi Abdelmalek
2012-9-4
%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
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.
Azzi Abdelmalek
2012-9-4
A is not an input argument;
A(idx) = ~A(idx);
it can't be calculated
Jan
2012-9-4
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 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!