• Register
0 votes

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

Please log in or register to answer this question.

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 28 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, 2020 LizzyM 6.1k points
0 votes
1 answer 129 views
Problem: Hello programmers! I am learning to program, and I just got a little confused about boolean values and logical operators. I tried to run the following program: #include<iostream> using namespace std; int main() { bool check = false && true || true; ... program, I got the following output: Condition evaluated to true I am so confused about how this condition evaluates to true. Any help?
asked Dec 18, 2020 Code Learner 6.5k points
0 votes
1 answer 6 views
Problem : Matlab Error : Z must be a matrix, not a scalar or vector.
asked Feb 25 Shovo210 3.9k points
0 votes
1 answer 45 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, 2020 Code Learner 6.5k points
1 vote
1 answer 732 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, 2020 Martin K 6.6k points
1 vote
1 answer 61 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, 2020 LizzyM 6.1k points
0 votes
1 answer 190 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, 2020 Ahsan 1.8k points
0 votes
2 answers 572 views
Problem : I am facing following error message: Index in position 1 is invalid. Array indices must be positive integers or logical values. My code is as follows: clc; close all; clear; PD=phantom('Modified Shepp-Logan'); padimage = [2,2]; PD= padarray(PD, ... : Index in position 1 is invalid. Array indices must be positive integers or logical values. Can anybody tell me why the issue is here?
asked Nov 24, 2019 alecxe 7.5k points
1 vote
2 answers 144 views
Problem : Recently I have joined a new project on Matlab. Please find below part of my Matlab code:   rawmb=ceil(myCount/22);      i = rawmb*16-15;      j = colmb*16-15;                        % dy is row(vertical) index             % dx is col( ... below warning: Integer operands are required for colon operator when used as index. Can some expert in Matlab help me in fixing above error?
asked Apr 20, 2020 stewart 4k points
0 votes
1 answer 17 views
Problem: iu must be specified for systems with more than one input.
asked Feb 18 charles mathews 3.9k points