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

bethel o
bethel o 2011년 2월 21일
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
Sean de Wolski
Sean de Wolski 2011년 2월 21일
Do you have a question?
Andreas Goser
Andreas Goser 2011년 2월 21일
I can't resist: Is it wired ot weird?
bethel o
bethel o 2011년 2월 21일
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 2011년 2월 21일
@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
Andreas Goser
Andreas Goser 2011년 2월 21일
I noticed that :-) - can't edit the comments

댓글을 달려면 로그인하십시오.

 채택된 답변

Paulo Silva
Paulo Silva 2011년 2월 21일

0 개 추천

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

bethel o
bethel o 2011년 2월 21일
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 2011년 2월 21일
I see your solution cares the duplications but this duplicate are fine. The issue is that the result i get is completely wrong
Paulo Silva
Paulo Silva 2011년 2월 21일
no the duplicated values aren't fine, when you expand something the borders shouldn't be duplicated
bethel o
bethel o 2011년 2월 21일
yep cheers that one is corrected. but the issue remains.
Paulo Silva
Paulo Silva 2011년 2월 21일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

bethel o
bethel o 2011년 2월 21일

0 개 추천

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

Paulo Silva
Paulo Silva 2011년 2월 21일
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 2011년 2월 21일
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
Paulo Silva
Paulo Silva 2011년 2월 21일
just use the matlab ismember function
doc ismember
Paulo Silva
Paulo Silva 2011년 2월 21일
or even better
tf=isempty(dim_set(dim_set==memb))
Paulo Silva
Paulo Silva 2011년 2월 21일
I forget the negation ~, this way should work
tf=~isempty(dim_set(dim_set==memb))
bethel o
bethel o 2011년 2월 21일
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.
Paulo Silva
Paulo Silva 2011년 2월 21일
Seem to be working fine here with all the changes I suggested
bethel o
bethel o 2011년 2월 21일
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 2011년 2월 21일
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.

댓글을 달려면 로그인하십시오.

질문:

2011년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by