how to delete particular values in this matrix ?
Show older comments
[a b]=[ 0.5708 0.0755 0 0 0 0 0
0 0 161.5569 0 84.9907 35.0193 17.0669];
i don't want the values before this
" 0
0 "
and my answer should be like this..
[c d]=[ 0 0 0 0
0 84.9907 35.0193 17.0669];
6 Comments
Image Analyst
on 9 Feb 2015
What do you mean by [a b] and [c d] - that is not MATLAB syntax. Also, do you know about the all() function?
Matlab111
on 9 Feb 2015
Stephen23
on 9 Feb 2015
But you are giving us code that does not work, and makes no sense in MATLAB:
>> [a b]=[ 0.5708 0.0755 0 0 0 0 0
0 0 161.5569 0 84.9907 35.0193 17.0669];
Too many output arguments.
Image Analyst is correct: this is not MATLAB syntax. You can assign your vector of values to only one variable (at a time). Why are you trying to allocate these vectors to both a and b , and c and d ?
Matlab111
on 9 Feb 2015
Michael Haderlein
on 9 Feb 2015
Edited: Michael Haderlein
on 9 Feb 2015
That's quite a lot of code for this question (and most of the code has nothing to do with the question). What is the criterion? Should everything be deleted until a column of zeros? Should the first three columns be deleted? Or is it something else?
case 1: (what Image Analyst was hinting at)
a=[ 0.5708 0.0755 0 0 0 0 0;
0 0 161.5569 0 84.9907 35.0193 17.0669];
allzeros=find(all(a==0));
b=a(:,allzeros:end);
case 2:
b=a(:,4:end);
If none of these cases are what you're looking for, please give detailed information on what you need and post only relevant code.
Matlab111
on 9 Feb 2015
Accepted Answer
More Answers (1)
Try this:
>> c = [0.7893,0.8337,0.1479,0,0,0.1479,0.9993];
>> X = sum(bsxfun(@(a,b)abs(a-b)<1e-6,c(:).',c(:)))<2;
>> X(1:find(c==0,1,'last')) = false;
>> c(X)
ans = 0.9993
As in my answer to your other related question , note that I did not use equality test == or unique with floating point numbers, but instead compared the difference of two values with some tolerance, in this case 1e-6. You can change the tolerance to best suit your problem.
7 Comments
You will have to explain that again. I ran your code, and got some vectors for allR11 and allR21. The code I proposed works: the indices removed all repeated values and all values up until and including the last zero. This is the expected behavior of my code, and it is what you requested in your comment to your original question.
What is happening that you do not expect?
Are you expecting to perform this on a matrix with two rows, and remove any column that contains a duplicate value in its own row, or is in front of any zeros?
Currently I have provided code that does exactly what you requested, so you will have to explain your exact situation in more detail if this does not resolve your problem.
Stephen23
on 9 Feb 2015
PS: are you planning on accepting any answer to your last question?:
Matlab111
on 9 Feb 2015
You can just repeat the method that I gave in my original answer for each row, and use the indices together:
>> A = [0.6902, 0.1634, 0.6987, 0, 124.7462, 20.0765, 23.4781, 194.6180; 0, 0, 0, 0, 171.3741, 147.7809, 46.3979, 129.3696];
>> X = sum(bsxfun(@(a,b)abs(a-b)<1e-6,A(1,:).',A(1,:)))<2;
>> Y = sum(bsxfun(@(a,b)abs(a-b)<1e-6,A(2,:).',A(2,:)))<2;
>> X(1:find(A(1,:)==0,1,'last')) = false;
>> Y(1:find(A(2,:)==0,1,'last')) = false;
>> A(:,X&Y)
ans =
124.75 20.076 23.478 194.62
171.37 147.78 46.398 129.37
No repeated values and with all terms up until the final zero removed.
I suspect that your code might actually be a lot neater and faster if you do not keep splitting your data up into smaller variables, but keep it together in one array, and just work with indices. In my experience this is often a much faster and more reliable way to manage data.
Stephen23
on 10 Feb 2015
I have replied to this in a new answer.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


