Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Comparing each element for two matrices

Subject: Comparing each element for two matrices

From: Arie Driga

Date: 10 May, 2010 11:37:05

Message: 1 of 4

Hi all,

I have two matrices, each with size of 5x11. I would like to compare each element in each matrix with a value.


I use if for this purpose:

if (Xh_channel <= 0.24 && Xt_channel >= 0.15)
         %pass = 1;
        for i = 1:size(x_var,1)
      Nu_mean=interp1(linspace(1,0.166667,size(y_var_nu_ch_1,1)),z_var_nu_ch_fine_1(i,:), x_var(i,:));
         end
end;

but.. I have this error message:

??? Operands to the || and && operators must be convertible to logical scalar values.

Error in ==> channel_heat_transfer at 81
      if ((Xh_channel <= 0.024) && (Xt_channel >= 0.015))

I hope my explanation is clear enough. Looking forward for some assistances here.

Thank you.

Subject: Comparing each element for two matrices

From: Sean

Date: 10 May, 2010 11:47:06

Message: 2 of 4

"Arie Driga" <a_driga@yahoo.com> wrote in message
> ??? Operands to the || and && operators must be convertible to logical scalar values.
>
> Error in ==> channel_heat_transfer at 81
> if ((Xh_channel <= 0.024) && (Xt_channel >= 0.015))

Without looking at anything else you're trying to do a scalar operation on a vector or matrix. Use a single '&' or '|' when doing this.

Good luck!

Subject: Comparing each element for two matrices

From: Steven Lord

Date: 10 May, 2010 13:17:56

Message: 3 of 4


"Arie Driga" <a_driga@yahoo.com> wrote in message
news:hs8r51$kjp$1@fred.mathworks.com...
> Hi all,
>
> I have two matrices, each with size of 5x11. I would like to compare each
> element in each matrix with a value.
>
>
> I use if for this purpose:
>
> if (Xh_channel <= 0.24 && Xt_channel >= 0.15)
> %pass = 1;
> for i = 1:size(x_var,1)
> Nu_mean=interp1(linspace(1,0.166667,size(y_var_nu_ch_1,1)),z_var_nu_ch_fine_1(i,:),
> x_var(i,:));
> end
> end;
>
> but.. I have this error message:
>
> ??? Operands to the || and && operators must be convertible to logical
> scalar values.
>
> Error in ==> channel_heat_transfer at 81
> if ((Xh_channel <= 0.024) && (Xt_channel >= 0.015))
>
> I hope my explanation is clear enough. Looking forward for some
> assistances here.

As the message indicates, && and || are only intended to be used for
_scalar_ inputs. Since your Xh_channel is a matrix, you cannot use && for
this purpose.

However, simply replacing && by & as Sean suggested may not be sufficient in
this case. That would only execute the loop body if ALL of the elements of
Xh_channel were between 0.15 and 0.24 inclusive, which is not what I believe
you want to do. See the remark "Nonscalar expressions" on the reference
page for IF for an explanation.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/if.html

If you want to operate only on those elements of Xh_channel that are in that
interval, use logical indexing.

indicesOfElementsInInterval = (Xh_channel >= 0.15 & Xh_channel <= 0.24);
elementsInInterval = Xh_channel(indicesOfElementsInInterval);

Now operate on the elements of elementsInInterval.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Comparing each element for two matrices

From: Arie Driga

Date: 10 May, 2010 14:28:24

Message: 4 of 4

 
> As the message indicates, && and || are only intended to be used for
> _scalar_ inputs. Since your Xh_channel is a matrix, you cannot use && for
> this purpose.
>
> However, simply replacing && by & as Sean suggested may not be sufficient in
> this case. That would only execute the loop body if ALL of the elements of
> Xh_channel were between 0.15 and 0.24 inclusive, which is not what I believe
> you want to do. See the remark "Nonscalar expressions" on the reference
> page for IF for an explanation.
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/ref/if.html
>
> If you want to operate only on those elements of Xh_channel that are in that
> interval, use logical indexing.
>
> indicesOfElementsInInterval = (Xh_channel >= 0.15 & Xh_channel <= 0.24);
> elementsInInterval = Xh_channel(indicesOfElementsInInterval);
>
> Now operate on the elements of elementsInInterval.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

I tried using your syntax

> indicesOfElementsInInterval = (Xt_channel >= 0.15 & Xh_channel <= 0.24);
> elementsInInterval = Xh_channel(indicesOfElementsInInterval);

Thank you! I think it works now..

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us