Question regards making a function in matlab involve matrices

1 回表示 (過去 30 日間)
Samious
Samious 2013 年 9 月 7 日
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 件のコメント
Samious
Samious 2013 年 9 月 7 日
I try that method but I still get some of the answer in the form of -1. Is there a way to script the function that the answer will only be 0s or 1s?
Walter Roberson
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
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

Roger Stafford
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 件のコメント
Samious
Samious 2013 年 9 月 7 日
So should I write this in matlab as function
C = noisify(M)
for M=[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]
end
size(M)=[m,n]
p=ceil(m*rand(1,n))+m*(0:n-1);
M(p) = 1-M(p);
end?
Walter Roberson
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
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.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by