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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by