Question regards making a function in matlab involve matrices
이전 댓글 표시
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일
Functions need to take parameter names such as
function C = noisify( original_matrix )
C = original_matrix - round(rand(7,4));
end
Samious
2013년 9월 7일
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
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
2013년 9월 7일
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.
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!