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 个评论
Image Analyst
2015-2-9
What do you mean by [a b] and [c d] - that is not MATLAB syntax. Also, do you know about the all() function?
Matlab111
2015-2-9
Stephen23
2015-2-9
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
2015-2-9
Michael Haderlein
2015-2-9
编辑:Michael Haderlein
2015-2-9
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
2015-2-9
采纳的回答
更多回答(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 个评论
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
2015-2-9
PS: are you planning on accepting any answer to your last question?:
Matlab111
2015-2-9
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
2015-2-10
I have replied to this in a new answer.
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



