Hii I am not understanding this code please help and explain me step by step and please explain me idx2 = [0, idx, 0] what does it do.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
idx = EV > 0;
if iscolumn(idx), idx = idx'; end;
idx2 = [0, idx, 0];
prePt = []; postPt = [];
for i = 2 : length(idx2)-1
if idx2(i-1) == 0 & idx2(i) == 1 & idx2(i+1) == 1
prePt = [prePt, i-1];
end
if idx2(i-1) == 1 & idx2(i) == 1 & idx2(i+1) == 0
postPt = [postPt,i-1];
end
end
segNum = length(prePt);
segment = [];
for k = 1 : segNum
segment(k,:) = [prePt(k), postPt(k)];
end
Accepted Answer
That bit of code concatenates zeroes onto both ends of the vector idx, and allocates this lengthened vector to a new variable idx2. Like this:
>> X = [5,6,7];
>> [0,X,0]
ans = 0 5 6 7 0
This is because the [] is a concatenation operator (not a list generation operator like lots of beginners think). Read this for more info on []:
9 Comments
Thanks Sir, But when I am trying to run this code it is showing me the error- Error in getSegment (line 9) idx2 = [0, idx, 0]
and when I am Running idx2=[0,idx,0] in command window it is showing me the erroe as- Error using horzcat Dimensions of matrices being concatenated are not consistent.
Why is this can you explain pls...thanks a lot for your help..
Please show us the complete error message. This means all of the red text.
For this code to work idx must be a vector, but it seems that idx is not a vector. If idx is either empty or a matrix then you may get this error. So check the size of idx like this:
size(idx)
and tell us what the result is. Your code is likely doing something like this:
>> X = ones(2)
X =
1 1
1 1
>> [0,X,0]
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Yes you r ryt, This is the size of idx-
>> size(idx)
ans =
1440 2
How to solve this issue pls help.
The variable EV should be a vector, but it is a matrix instead. You do not tell us where EV comes from or how it is defined, so you will have to have a look at where EV is created and check whey it is the wrong size.
Alternatively you could try running this before your code:
EV = EV(:);
which converts the matrix to a vector, but there is no guarantee that its number of elements will suit the rest of your algorithm.
Thanks a lot Sir it is now running after doing EV=EV(:); can u pls explain me this foe loop
for i = 2 : length(idx2)-1 if idx2(i-1) == 0 & idx2(i) == 1 & idx2(i+1) == 1 prePt = [prePt, i-1]; end if idx2(i-1) == 1 & idx2(i) == 1 & idx2(i+1) == 0 postPt = [postPt,i-1]; end
You have asked this in your other question, so that question is the correct place for explaining how the code works. Please do not mix questions and repeat questions in multiple places: it just makes it harder for us to keep track of what answers you have been given, which makes it hard for us to give the best help.
ALSO on this forum it is considered polite to accept the answer that best resolves your question. This gives point to the volunteers who give their time to help you.
Thanks a lot sir I have accepted As i am new so I dnt know all these things
one more thing sir pls make me understand this line >>segment(k,:) = [prePt(k), postPt(k)] what output it will give.
Thank you so much for giving time and helping me out and making me understand things
In my answer I explained that [] is a concatenation operator. This means that the code [prePt(k),postPt(k)] concatenates the k-th element of prePt and postPt together, and allocates this to the k-th row of segment.
More Answers (0)
Categories
Find more on Historical Contests in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)