A little help?

Hi there,
I am having a little problem with one of the questions for an assignment I have. I have less than basic knowlage of MALAB so this assignment is to break me in so to say.
The question states that I need to create a matrix with with the input values clamped in the range +- 1.
Any help would be much appreciated! :D

1 个评论

Aadam
Aadam 2011-12-11
Hi guys thanks for helping.
I don't think I made myself very clear or explained my puzzle very clearly. For this I apologise
I will have a second attempt now.(Please bare in mind i am not a very experianced programer):
On my question sheetI am given a return property that is defined as oneabs. This property needs to return the input values clamped in the range of +-1.
Now I think this is asking me to return everything inbeetween and clip everything higher than +1 as 1, and everything <-1 as -1.
I hope this is a little clearer.

请先登录,再进行评论。

 采纳的回答

Aadam: Based on your latest comment to your original question I would just make my code into a function. In file test.m:
function test()
% Generate sample data.
M = 10*rand(8) - 5; % Sample matrix.
% Clip the data to the range -1 to +1
oneabs = Clip(M)
% Clip to range (-1 to +1) if outside that range.
% Leave values alone if within that range.
function oneabs = Clip(M)
oneabs = M; % Initialize.
oneabs (M < -1) = -1;
oneabs (M > 1) = 1;

更多回答(5 个)

Matrix=[-0.1,0.2;-0.5,0.9]
Based on your latest update in comments, here is a solution
InMatrix=4*rand(5)-2;
OutMatrix=min(InMatrix,1);
OutMatrix=max(OutMatrix,-1)

1 个评论

Jan
Jan 2011-12-11
@Fangjun: "create a matrix with with the input values clamped in the range +- 1". Yes! I like the deeper message of this answer. +1
@Aadam: I do not think, that this answer solves your problem.

请先登录,再进行评论。

Matrix = 2*rand(10,10) - 1

5 个评论

Aadam
Aadam 2011-12-11
After doing this i recieve an output: oneabs: [1x1863 char]
my matric is: A = [1,2,3;4,5,6;7,8,9];
B = A - 1;
C=2*B./max(B(:))-1;
Aadam
Aadam 2011-12-11
wow erm could you explain this please?
Follow this:
B = A - min(A(:));
C=B./max(B(:));
D=2*C;
E=D-1;
You're scaling his data, which he didn't ask for.

请先登录,再进行评论。

M = 10*rand(8) - 5; % Sample matrix.
% Now clip to range (-1 to +1)
M(M<-1) = -1;
M(M>1) = 1;

4 个评论

Aadam
Aadam 2011-12-11
I am pretty sure the output is given from entering a file containing a matrix through my program and then displaying all integers >1 as +1, all integers <-1 as -1 and everything inbetween as its decimal value.
i.e.
2 = +1
-2 = -1
0.5 = 0.5
-0.5= -0.5 et.al
Yeah, so? That's what this code does - leaves values between -1 and 1 alone and clips values outside that range. If you need to read the file, then say so - you can use things like csvread, dlmread, etc. At first you said you need to create a matrix, but I guess you could create it by reading a file. To display, merely put the name of the array on its own line, like this:
M
Aadam
Aadam 2011-12-11
have tried this but had an answer of oneabs: [1x1863 char]. a little confused. :s
No you didn't try it. You didn't try my code because I never mentioned oneabs. In fact I just copied and pasted my code into MATLAB, now that I'm on a computer that has it, and it ran fine and did exactly what I said and what you asked. You'd need to post your code for us to figure out what you did wrong.

请先登录,再进行评论。

Jan
Jan 2011-12-11
Another approach:
M = 10*rand(8) - 5; % Sample matrix. (Thanks IA!)
M = max(min(M, 1), -1);
The Matrix of interest of poster is:
A = [1,2,3;4,5,6;7,8,9];
and the soloution is the following:
B = A - min(A(:));
C=2*B./max(B(:))-1;

4 个评论

Why do you say that? I don't agree with that at all. You're scaling the data to make it fit in the range. Data values in the range will get changed if you do that. He didn't say anything about wanting to scale his data - he just wants to clamp/clip his data that went outside of the range.
Jan
Jan 2011-12-11
@Mohsen: Do you have personal contact to the OP? How do you know that the poster asks for [1,2,3; 4,5,6; 7,8,9]?
@ Jan: Please take a look at Aadam's comment on my first post above, were he introduces his Matrix and asks for extra information on my code.
@ Image Analyst: Looking at the A matrix Aadam has in mind, the way you go is not correct!! The matrix elemets are between 1 and 9.
Mohsen, that matrix was in response to your question and as I mentioned it's not what any of us suggested because he somehow came up with some new matrix oneabs. So who knows why he cam up with that. Now if his matrix were really that (in the range 1-9) the simplest solution would simply be M = ones(1, length(M)); Of course he'd have no values less than 1 so there's be no reason to clip there like he originally required. But he said he DID need to clamp to -1 in his question and subject line. Neither Jan nor I sees anything at all where he says "scale my data" like you gave but we do see where he says "clamp my data." Though I do agree that your code does scale, which is fine if that's what he wants (despite saying something to the contrary). Aadam, please clarify: do you want scaling or clamping?

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by