Remap values of 256x256 array based on ranges

done like this
if (n_rounded>=-90.0) && (n_rounded<-67.5)
n_rounded=-90;
elseif (n_rounded>=67.5) && (n_rounded<90.0)
n_rounded=-90;
elseif (n_rounded>=-67.5) && (n_rounded<-22.5)
n_rounded=-45;
elseif (n_rounded>=-22.5) && (n_rounded<22.5)
n_rounded=0;
elseif (n_rounded>=22.5) && (n_rounded<67.5)
n_rounded=45;
end
getting error Operands to the and && operators must be convertible to logical scalar values.

1 Comment

Matt J
Matt J on 22 Feb 2016
Edited: Matt J on 22 Feb 2016
  1. You cannot apply && and to non-scalars. You could use & and |.
  2. if...else commands do not distribute through the elements of an array

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 22 Feb 2016
Edited: Stephen23 on 22 Feb 2016
The basic problem is that both logical short-circuiting operators || and && operate on scalar value only. Here is an example:
>> false || true % both operands are scalar -> okay!
ans =
1
>> false || [true,false] % scalar and vector -> error!
Operands to the || and && operators must be convertible to logical scalar values.
So clearly your n_rounded is not a scalar, as the inputs to the || and && operators are non-scalar. You can remove this error this by using standard or | or and & operators. Then you will meet another behavior that confuses many beginners: all elements must be true for an if to run:
if [true,false]
disp('this will never run')
end
if [true,true]
disp('this code will run')
end
Alternative Solution
Simply vectorize whole thing and use histc:
>> n_rounded = 180*rand(5)-90
n_rounded =
-14.8919 -1.9345 50.4454 -66.2448 -47.7396
-81.0622 -29.2105 -19.8470 79.5691 -26.4315
72.4889 72.0097 -46.4956 82.1042 57.8149
80.0617 -23.5356 -17.2958 13.5375 -87.2274
-1.6445 -69.9835 -72.6382 -79.2397 -82.2557
>> edg = [-90,-67.5,-22.5,22.5,67.5,90+eps];
>> rep = [-90,-45,0,45,90,NaN];
>> [~,idx] = histc(n_rounded,edg);
>> rep(idx)
ans =
0 0 45 -45 -45
-90 -45 0 90 -45
90 90 -45 90 45
90 -45 0 0 -90
0 -90 -90 -90 -90

5 Comments

thanks for reply sir getting error
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
how to solve
Stephen23
Stephen23 on 22 Feb 2016
Edited: Stephen23 on 22 Feb 2016
"How to solve"
My answer works without error: I tested it. You are getting an error because of something you wrote, but you have not shown this to us. How am I supposed to know what you wrote if you do not show it? Sorry, but my mind-reading skills are a bit rusty, and I cannot read your computer screen, so it is difficult to diagnose what error you have in your code when you do not show us that code.
nice sir error resolved nice code but when opening idx from workspace it is indicatiing number wise 1,2,3,4,5 like 1 represnting -90,2 represnting -45 and so on ,how to get values in workspace by anglewise.only in command window its showing angle wise
If it is showing in the command window then it is also in your workspace. Your request is not totally clear to me, but I think you might just need to allocate the output to a variable:
out = rep(idx);
correct done sir

Sign in to comment.

More Answers (1)

Matt J
Matt J on 22 Feb 2016
Edited: Matt J on 22 Feb 2016
[~,~,bins]=histcounts(n_rounded,[-90,-67.5,-22.5,22.5, 67.5,90] );
targetValues=[-90,-45,0,45,-90];
n_rouned=targetValues(bins);

1 Comment

error Undefined function 'histcounts' for input arguments of type 'double'.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Asked:

Ram
on 22 Feb 2016

Commented:

Ram
on 22 Feb 2016

Community Treasure Hunt

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

Start Hunting!