|
|
How to if-else in 1 line in matlab like C --- x = (d < D0)?20:30;---
I would like to do if-else in 1 line in matlab but I don't know if MATLAB has this feature.
My C program is:
x = (d < D0)?20:30;
which is equal to
if (d < D0)
x = 20;
else
x = 30;
Please suggest how can I do this for 1 line in matlab??? Thank you very much.
|
|
0
|
|
|
|
Reply
|
Aui
|
12/4/2009 5:15:19 AM |
|
On 4 Dez., 06:15, "Aui " <littlebearproj...@gmail.com> wrote:
> I would like to do if-else in 1 line in matlab but I don't know if MATLAB has this feature.
>
> My C program is:
>
> x = (d < D0)?20:30;
>
> which is equal to
> if (d < D0)
> x = 20;
> else
> x = 30;
>
> Please suggest how can I do this for 1 line in matlab??? Thank you very much.
why does everyone wants to make his code unreadable?
if (d < D0) x = 20; else x = 30;end;
or for this special case
x = (d < D0)*10+20;
|
|
0
|
|
|
|
Reply
|
Justus
|
12/4/2009 5:51:17 AM
|
|
Thank you Justus.
Actually, I want to apply on matrix and the code is just for example.
The real one is
F_mb = G./((D <= 500).*H1);
F_mb = G./((D > 500).*H2);
where D, G, H1 and H2 is matrix with the same size.
I just would like to combine them into a single line. Is that possible.
|
|
0
|
|
|
|
Reply
|
Aui
|
12/4/2009 6:02:05 AM
|
|
Well, actually, the above code is that I would like to avoid divided by zeros.
but the above code is still incorrect.
My goal is that I would like to divide G with H only the position that H element is not zeros so that will not produce divided by zero problem.
F_mb = G./((H1 ~= 0).*H1);
but in this case, even H1 is 0, it still divided by zeros.
|
|
0
|
|
|
|
Reply
|
Aui
|
12/4/2009 6:17:01 AM
|
|
In this thread you seem to be unclear as to what exactly you want. Does this do it?
F_mb = H1~=0;
F_mb = G(F_mb)./H1(F_mb)
This *could* be done in one line, but it wouldn't be as efficient.
|
|
0
|
|
|
|
Reply
|
Matt
|
12/4/2009 7:01:24 AM
|
|
Hello, Matt Fig,
Thank you. It seems to produce what I want but the dimension of the result matrix change!!!! How can I keep the same dimension?
I applied your code for testing a small size matrix
F_mb = H1~=0;
F_mb = G(F_mb)./H1(F_mb)
-------------------
>> G = [2 2; 4 4];
>> H1 = [0 0; 10 10];
>> F_mb = H1~=0
F_mb =
0 0
1 1
>> F_mb = G(F_mb)./H1(F_mb)
F_mb =
0.4000
0.4000
------------------
What I expect the result should be
F_mb =
2 2
0.4000 0.4000
Thank you
|
|
0
|
|
|
|
Reply
|
Aui
|
12/4/2009 7:39:20 AM
|
|
Well that does add a little bit of complication doesn't it?
% Data
G = [2 2; 4 4];
H1 = [0 0; 10 10];
% Engine
F_mb = G;
idx = H1~=0;
F_mb(idx) = G(idx)./H1(idx)
|
|
0
|
|
|
|
Reply
|
Matt
|
12/4/2009 10:48:03 AM
|
|
Hi Matt Fig,
Thank you so much. That's what I want (but I cannot do it myself ^^).
|
|
0
|
|
|
|
Reply
|
Aui
|
12/4/2009 3:51:03 PM
|
|
|
7 Replies
319 Views
(page loaded in 0.049 seconds)
Similiar Articles: if-else-end in anonymous functions - comp.soft-sys.matlab ...How to if-else in 1 line in matlab like C --- x = (d D0)?20:30 ... if (d < D0) x = 20; else x = 30;end; or for this special case x ... the 3-D array Right_matrix(x,y,z ... Two graphs in one figure - comp.soft-sys.matlabMathew: 12/10/2009 8:40:20 PM ... Combine graphs - comp.soft-sys.matlab s=openfig('model1.fig'); c ... graph window - comp.soft-sys.matlab ... hey, i want to draw two line ... Finding common lines between text files - comp.unix.programmer ...How to if-else in 1 line in matlab like C --- x = (d D0)?20:30 ... Finding common lines between text files - comp.unix.programmer ..... txt: a b c d e b.txt: c d a What ... Compiling MCR C++ dll with VS2010 - comp.soft-sys.matlab ...Go to line 346 of c:\program files\matlab\r2008b\extern\include\matrix.h ... to edit the .h files provide by someone else ... Considering my problem, I'd like to use this ... Including an area into a circle - comp.soft-sys.matlabImageAnalyst: 1/20/2011 2:43:24 AM ... of circles - comp.soft-sys.matlab split line on a ... into the image, what value would you like the ... MathWorks - MATLAB ... efficiency in awk - comp.lang.awkHow to if-else in 1 line in matlab like C --- x = (d D0)?20:30 ... efficiency in awk - comp.lang.awk Doug: 10/30/2003 7:06:20 PM ... wrote: > - The -Wexec command line ... fread and float - comp.soft-sys.matlab... input~=0) ; % 0 for zero values else +1 ... the description is sounds a lot like the VAX D_FLOAT format. What version of MATLAB are ... you have provided to give you a line-by ... awk challenge: sort array using only for ... in ... - comp.lang ...... possible to do it in time n*log(n) like ... to stream # BEGIN { a["a"]=20 a["b"]=5 a["c"]=15 a["d"]=7 ... vmin ) { usei = 1; } } else { usei = 1 ... How to sort array - comp.lang.awk... echo "" | awk '{ a[2] = "d 1 ... as Dave said, change that line to: awk 'BEGIN{ > a[2] = "d 1 ... Sort cell array - comp.soft-sys.matlab Hi, I would like ... How to check whether malloc has allocated memory properly in case ...12/16/2010 4:30:20 PM ... general-purpose one like ... or even at the same one, > especially in academia. .... I'd really emphasize the last line in the above--in my 30 ... Tutorial 2 Programming in MATLAB20 25 30 35 40 x y The level curves of z = y2 - x2. 8 8 ... for i=1:5 h1_line = plot(x,sin(i*x)); set(h1_line ... exercise you are to write MATLAB function d = dsc(c) that takes a one ... MATLAB - The Language of Technical ComputingMATLAB is a high-level technical computing language and interactive environment for algorithm development, data visualization, data analysis, and numerical computation. 7/30/2012 10:11:25 AM
|
|
|
|
|
|
|
|
|