Question regards making a function in matlab involve matrices
1 次查看(过去 30 天)
显示 更早的评论
Hi, I'm a completely newbies to Matlab, just got a student version a few days ago. And I was asked to solve this question using matlab. Can anyone guide me in making a function to satisfy the condition below.
Write a MATLAB function called noisify which accepts a 7 x 4 matrix of data and which returns a 7 x 4 matrix of corrupted data, where each column has only one bit randomly flipped to simulate an error in transmission occurring.
Hint: The MATLAB functions rand, round or randi may be useful.
Best Regards
Edit: I forgot to add more information about the question, the initial input is
[1 0 01; 0 0 1 0; 1 1 0 1; 0 0 1 1; 1 1 0 0; 0 1 1 1; 1 0 0 0]
a 7 by 4 matrice, my goal is to find a function that will corrupt this matrix while at the same time maintaining a 7x4 function and a binary number as output (0 or 1).
Is it possible for me to write a script like
function [C] = noisify([1 0 0 1; 0 0 1 0; 1 1 0 1; 0 0 1 1; 1 1 0 0; 0 1 1 1; 1 0 0 0])
%C is a 7 by 4 matrix which has been corrupted
C= [1 0 0 1; 0 0 1 0; 1 1 0 1; 0 0 1 1; 1 1 0 0; 0 1 1 1; 1 0 0 0] - round(rand(7,4))
end
However I don't know how to limit the answer to number 0 or 1 only. How do I do that?
3 个评论
Walter Roberson
2013-9-7
Consider the possibility that your expression is wrong :-)
If you are starting with a 0 and it flips, then how much do you need to subtract? If you are starting with a 1 and it flips, then how much do you need to subtract?
回答(3 个)
Walter Roberson
2013-9-7
hint:
t = round([rand; rand; rand])
sum(t)
Execute that code several times in a row and look at the patterns you get
0 个评论
Roger Stafford
2013-9-7
Let M be your 7 x 4 matrix of 1's and 0's.
[m,n] = size(M);
p = ceil(m*rand(1,n)) + m*(0:n-1);
M(p) = 1-M(p);
2 个评论
Walter Roberson
2013-9-7
No, there are several problems with that. For example, you should be working with the matrix that is passed in rather than with a fixed matrix.
Image Analyst
2013-9-7
编辑:Image Analyst
2013-9-7
I shouldn't be doing this, but try this:
function test2()
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
C = [1 0 0 1; 0 0 1 0; 1 1 0 1; 0 0 1 1; 1 1 0 0; 0 1 1 1; 1 0 0 0]
% Make it noisy by flipping one bit in each column.
noisyC = noisify(C)
function C = noisify(original_matrix)
[rows, columns] = size(original_matrix)
% Get the row in each column that you're going to invert.
randomRowsToFlip = randi(rows, [columns, 1])
% Initialize
C = logical(original_matrix);
% Now flip the appropriate row in each column.
for col = 1 : columns
C(randomRowsToFlip(col), col) = ~C(randomRowsToFlip(col), col);
end
Now you can't turn it in, or else I'd get the A. Bad Image Analyst - I've slapped my hand as punishment.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!