Matlab does wired stuff

This is part of a function that is doing what i called wired. The vector 'dim_set' in this snippet should expanded on both side depending on the value of the scalar 'const'. I have no idea while the result is wired. Pls help!
% expand dim_set by 1 or a const on each side
dim_set=[67.5:100.5];
const=20;
dim_set %befor
dim_set=floor(dim_set);
if(dim_set(1)>1)
dim_set=[dim_set(1)-const:dim_set(1),dim_set];
end
dim_set=[dim_set,dim_set(end):dim_set(end)+const];
% See befoer and after values of dim_set to see this
dim_set %after

6 Comments

bethel o
bethel o on 21 Feb 2011
Hmm i have just ran this snippet on the command window and it just worked fine, but the exact code is refusing to work in my function
Do you have a question?
I can't resist: Is it wired ot weird?
bethel o
bethel o on 21 Feb 2011
sorry my question is, why dim_set output not right when ran inside my function, but hover works when i change the scalar 'const' from 20 to 1. I will post the function
bethel o
bethel o on 21 Feb 2011
@Andreas Goser. i meant weird. I have been coding for stegers algorithm constantly for one week now, so am tired. And by the way is it 'ot weird' or 'or weird'?. lol there
I noticed that :-) - can't edit the comments

Sign in to comment.

 Accepted Answer

dim_set=67.5:1:100.5
const=20;
dim_set %before
dim_set=floor(dim_set); %have no clue what's the purpose of this line
dim_set=[dim_set(1)-const:dim_set(1)-1 dim_set]; %expand left
dim_set=[dim_set dim_set(end)+1:dim_set(end)+const]; %expand right
% See before and after values of dim_set to see this
dim_set %after

5 Comments

bethel o
bethel o on 21 Feb 2011
thanks Paulo but this dim_set=floor(dim_set); is needed to as i only want integer values for the next part of the function. I have just post the complete function. Thanks again
bethel o
bethel o on 21 Feb 2011
I see your solution cares the duplications but this duplicate are fine. The issue is that the result i get is completely wrong
no the duplicated values aren't fine, when you expand something the borders shouldn't be duplicated
bethel o
bethel o on 21 Feb 2011
yep cheers that one is corrected. but the issue remains.
You were correct, now that I know what your function does I came to the conclusion that the borders are irrelevant but there's no need to waste a little more memory with duplicated values.

Sign in to comment.

More Answers (1)

bethel o
bethel o on 21 Feb 2011
function tf=intersect_l(dim_set,memb)
% expand dim_set by 1 or a const on each side
%dim_set=[67.5:100.5];
const=20;
dim_set %befor
dim_set=floor(dim_set);
if(dim_set(1)>1)
dim_set=[dim_set(1)-const:dim_set(1),dim_set];
end
dim_set=[dim_set,dim_set(end):dim_set(end)+const];
dim_set %after
if(intersect(dim_set,memb))
tf=1;
else
tf=0;
end
end

9 Comments

You are mixing the function and the function call in the same code and you don't explain exactly the purpose of your function.
Are you trying to see if dim_set is present inside memb?
bethel o
bethel o on 21 Feb 2011
sorry again. am trying to see if memb is present in dim_set. memb is just a scalar and dim_set is a vector. But the part that is misbehaving is the part befor the
if(intersect(dim_set,memb))
tf=1;
else
tf=0;
end
just use the matlab ismember function
doc ismember
or even better
tf=isempty(dim_set(dim_set==memb))
I forget the negation ~, this way should work
tf=~isempty(dim_set(dim_set==memb))
bethel o
bethel o on 21 Feb 2011
but the problematic part is the part that expands the dim_set. dim_set needs to be expanded. I have been coding regularly in matlab for over a year now but the output of that simple function is just weird. You can try the function while checking the values of dim_set for before and after expansion. I was thinking that some1 can pick up what i am doing fundamentally wrong.
Seem to be working fine here with all the changes I suggested
bethel o
bethel o on 21 Feb 2011
Thanks a lot Paulo. I have also tried it and it works, outside of its parent function (because its a sub function). I have no idea why it dosn't work for certain values of 'const' when ran through the parent function but works for all values of 'const' when ran as a function on its own.
I will have a sleep and come back to it.
Thanks alot for your assistance.
Bethel
bethel o
bethel o on 21 Feb 2011
Hey Paulo, I did not realize that in programing if I have something like
IF(i && j), j will not be checked if i =false . this was simply the problem I was having. I was checking the output of j and only saw it some times (i and j are functions that returns boolean in my program). I did not know that the reason I was not seeing j sometimes was because sometimes i =false.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!