how to delete particular values in this matrix ?

[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 Commenti

What do you mean by [a b] and [c d] - that is not MATLAB syntax. Also, do you know about the all() function?
Image Analyst- After running my loop i will get "[a b]" and similarly i'm storing this values in "[c d]" that's it..sir
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 ?
Stephen Cobeldick- just run this code that i attached below, you will get the values.."[allR11;allR21]" this is the matrix that i want to modified.
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.
Michael Haderlein- sir, don't go for code just see below i given some example.
question:
c=[0.7893 0.8337 0.1479 0 0 0.1479 0.9993];
1.now i should delete the repeated values in that 'c'.
2.i should delete the values that is displayed before the zeros.
3.And finally i should delete zeros also.
Expected output:
d=[0.9993];

Accedi per commentare.

 Risposta accettata

To solve your (constantly changing) requirement stated in your last comment to my original answer, try this code:
allR111 = [allR11;allR21];
Y3 = true(1,size(allR111,2));
Y3(1:find(abs(allR111(1,:))<1e-6,1,'last')) = false;
Y3(1:find(abs(allR111(2,:))<1e-6,1,'last')) = false;
allR111(:,Y3)

Più risposte (1)

Stephen23
Stephen23 il 9 Feb 2015
Modificato: Stephen23 il 9 Feb 2015
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 Commenti

Matlab111
Matlab111 il 9 Feb 2015
Modificato: Matlab111 il 9 Feb 2015
Stephen Cobeldick- sir, just see my code that i attached below, just go for last line no-395 that i have highlited in that i modified according to your case, as you said above, in that i'm getting for only 1st case for " allR11 " and i want to get similarly for "allR21" also and i tried but i'm not getting.
Note: sir, just type this in command window and see
1. "allR11" -this is 1st case. (i'm getting for this case)
2. "allR21" -this is 2nd case. (i'm not getting for this case)
sir, if not understanding clearly just ask me once again..
Stephen23
Stephen23 il 9 Feb 2015
Modificato: Stephen23 il 9 Feb 2015
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.
Stephen Cobeldick- sir go for this one " 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? "
yes sir i'm expecting to perform this on a matrix with two rows,
1. i want to remove duplicate values.
2. i want remove the values that is displayed before the zeros.
3. i want to delete the zeros also.
Note: By taking matrix with two rows, you will be getting like this
ans=[ 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]
and i'm expecting output is like this
ans=[124.7462 20.0765 23.4781 194.6180
171.3741 147.7809 46.3979 129.3696]
sir,just run this code you will get only "[allR11;allR21]" this that i want to modify
still if you are not clear you can ask me once again...
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.
Matlab111
Matlab111 il 9 Feb 2015
Modificato: Matlab111 il 9 Feb 2015
Stephen Cobeldick- sir, i try your logic with my main code ya i'm getting but, actually i want to get like this, just see below... when "r=0" i'm getting correct values that's what i expected but when "r=1" i'm not getting an expected values.
after applying your approach that you given above
and i want the answer should be like this, from column 40 to 71, no problem if it's a repeated values and i want get only that red marked part
Note: i'm getting for correct answer when "r=0", no problem if the values repeated and i have attached my modified code below..
if you not clear just ask me once again.
I have replied to this in a new answer.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by