• Register
0 votes
156 views

Problem :

I have very simple problem that currently looking for the fast implementation in Matlab. I have below array of values:

 b = floor(rand(5,5).*255)

I also have the similarly sized threshold array :

b_thresh = floor(rand(5,5).*255)

I want to use the value of b unaltered. I thought I can use something like the below:

b(b>= b_thresh .* 0.4 && b <b_thresh.* 0.5) = ((b - b_thresh.*0.4)/(b_thresh.*0.5 b_thresh.*0.4)) .* b;

But, I am facing below error :

Operands to || and && operations must be convertible to logical scalar values

How can I resolve the issue?

6 5 3
7,540 points

1 Answer

0 votes

Solution :

Regarding your first problem : The thing about the && is that it can operate only on the scalars, whereas the & can operate on arrays as well. So you should change your && to & to make it work.
 

Now regarding your second problem : The number of the elements on your left is different because you are trying to use the indices and on your right you are trying to work with the entire matrix b and b_thresh.

So you need to use the indices in both sides, so I suggest you to store it in the variable and then use it as the array subscript, along below lines:

myidx = (b >= b_thresh*0.4 & b < b_thresh*0.5);
b(myidx) = ((b(myidx)-b_thresh(myidx)*0.4) ./ (b_thresh(myidx)*0.5*b_thresh(myidx)*0.4)) .* b(myidx);

I hope it solves your issue.

9 7 4
38,600 points

Related questions

1 vote
1 answer 23 views
23 views
Problem: I am new in programming so I tried python practice I tried to figure out f any operator represents the logical and. in python programming can anybody help?
asked Apr 2 LizzyM 6.1k points
1 vote
1 answer 40 views
40 views
Problem: For the following code : E = [5,200]; Selected edge values X = imread('LENNA128.bmp'); N = length(X); Y = false(N+2); for k = 1:numel(E); Y(2:end-1,2:end-1) = X==E(k); Z = Y(1:end-2,2:end-1) | Y(3:end,2:end-1) | Y(2:end-1,3:end) ... )/2); end I get the error message: In an assignment A(:) = B, the number of elements in A and B must be the same Can anyone help me out to solve this problem.
asked Apr 9 LizzyM 6.1k points
0 votes
1 answer 5 views
5 views
Problem: I am learning logical operators in C# and I came across &,&& and |, ||. I am confused about what is the difference in both of these representations?
asked Oct 15 Sheeza 3.9k points
1 vote
1 answer 355 views
355 views
Problem : I am learning R. I am trying to make the subset of my data frame in the R which is based on one or the other value available in my column of the factors but because of some unknown reason to me I am unable to use | operator with the factor values. If I try to ... != "c" as it will not work. I am stuck with above error from past two days. Please guide me in fixing above R error for me.
asked May 12 Martin K 6.6k points
0 votes
1 answer 54 views
54 views
Problem: I am trying to plot two vectors in MATLAB and it keeps giving me the error that error in plot vector must be same length. What to do? Some one please help.
asked Oct 10 Ahsan 1.8k points