How to write a simple MATLAB code for simplification of if.. else.. code

My program has a vector x=(x1,x2,...xn) and I want to compare xp with the values of the vector x. I wrote the following code in my program. Can we simplify the following code using any matlab builtin functions
if (xp<=x(1)) s=1;
else if xp>=x(n) s=n;
else
for i=2:n-1
if xp>=x(i) && xp<x(i+1) s=i;
end
end
end
end

5 件のコメント

madhan ravi
madhan ravi 2018 年 9 月 13 日
What’s your input x and your desired output? Give an example .
PJS KUMAR
PJS KUMAR 2018 年 9 月 13 日
編集済み: PJS KUMAR 2018 年 9 月 13 日
  • x=[1 2 3 4 5 6] and the values represent the row numbers of a matrix.
  • xp is my input.
  • if xp is 1 or below 1, i have to select row 1 or matrix
  • if xp is in between any i and i+1 row, i have to select i row. and so on.
  • if xp is n or above n, i have to select row n.

The input is any value xp, and the output should be the number of the row in a matrix

Rik
Rik 2018 年 9 月 13 日

Does this code return the result that you want? Is it right that your output is only a scalar? Also, there is a big difference between else if and elseif. Below you find two blocks of code, which is the one you mean?

if (xp<=x(1))
    s=1;
else
    if xp>=x(n)
        s=n;
    else
        for i=2:n-1
            if xp>=x(i) && xp<x(i+1)
                s=i;
            end
        end
    end
end

Or

if (xp<=x(1))
    s=1;
elseif xp>=x(n)
    s=n;
else
    for i=2:n-1
        if xp>=x(i) && xp<x(i+1)
            s=i;
        end
    end
end
Walter Roberson
Walter Roberson 2018 年 9 月 14 日
What value is to be selected if xp > x(1) & xp < x(2) ?
Your pseudocode reserves the value 1 for values no greater than x(1), and starts at 2 for values at least as large as x(2), leaving open the question of what should happen for values between the two.
PJS KUMAR
PJS KUMAR 2018 年 9 月 14 日
for xp > x(1) & xp < x(2) i have to select x(1)

サインインしてコメントする。

回答 (3 件)

Walter Roberson
Walter Roberson 2018 年 9 月 14 日
[~, s] = histc(xp, [-inf x(2:end) inf]);
selected_x = x(s);
This assumes x is in increasing order.

3 件のコメント

PJS KUMAR
PJS KUMAR 2018 年 9 月 14 日
what is the purpose of [~,s]
Walter Roberson
Walter Roberson 2018 年 9 月 14 日
s is exactly the value you calculate in your pseudocode, 1 for values too low, n for values above the range, and so on.
If you are asking about the construct [~,s] then it is the same as if you had coded
[SoMEVarIaBlEiWILLnoTuSE, s] = histc(xp, [-inf x(2:end) inf]);
clear SoMEVarIaBlEiWILLnoTuSE
That is, it says that you want to ignore the output in that position. Like many functions, histc returns multiple outputs that have different purposes; the first output of histc is the bin counts (because histc was primarily intended to aid in computing histograms.)
Rik
Rik 2018 年 9 月 14 日
A minor edit makes removes the assumption of being ordered.
temp_x=sort(x);
[~, s] = histc(xp, [-inf temp_x(2:end) inf]);
selected_x = temp_x(s);

サインインしてコメントする。

Fangjun Jiang
Fangjun Jiang 2018 年 9 月 13 日
編集済み: Fangjun Jiang 2018 年 9 月 13 日
x=10:10:100;
xp=29;
n=numel(x);
interp1([-realmax,x,realmax],[1,1:n,n],xp,'previous')

5 件のコメント

PJS KUMAR
PJS KUMAR 2018 年 9 月 13 日
What is 29
Fangjun Jiang
Fangjun Jiang 2018 年 9 月 13 日
It is an example value of your xp.
PJS KUMAR
PJS KUMAR 2018 年 9 月 14 日
編集済み: PJS KUMAR 2018 年 9 月 14 日
x=[-1 -1.2 -1.4 -1.6 -1.8 -2]; i.e., the values of x are in decreasing order
  • xp=-1.3 (which is in between -1.2 and -1.4) i have to select the row with -1.2, and the output should be row number 2
  • xp=-2.5 , (-2.5 < -2) i have to select the row with -2, and the output should be row number 6
  • xp=1, (1 > -1) i have to select the row with -1, and the output should be row number 1
PJS KUMAR
PJS KUMAR 2018 年 9 月 14 日
Example - 1
x=[1 1.2 1.4 1.6 1.8 2]; i.e., the values of x are in increasing order
  • xp=1.3 (which is in between 1.2 and 1.4) i have to select the row with 1.2, and the output should be row number 2
  • xp=2.5 , (2.5 > 2) i have to select the row with 2, and the output should be row number 6
  • xp=0.5, (0.5 < 1) i have to select the row with 1, and the output should be row number 1
Example - 2
x=[-1 -1.2 -1.4 -1.6 -1.8 -2]; i.e., the values of x are in decreasing order
  • xp=-1.3 (which is in between -1.2 and -1.4) i have to select the row with -1.2, and the output should be row number 2
  • xp=-2.5 , (-2.5 < -2) i have to select the row with -2, and the output should be row number 6
  • xp=1, (1 > -1) i have to select the row with -1, and the output should be row number 1
Fangjun Jiang
Fangjun Jiang 2018 年 9 月 14 日
x=[-1 -1.2 -1.4 -1.6 -1.8 -2];
xp=[-1.3, -2.5, 1];
n=numel(x);
index=interp1([realmax,x(2:end),-realmax],[1:n,n],xp,'next')
yp=x(index)
x=[1 1.2 1.4 1.6 1.8 2];
xp=[1.3, 2.5, 0.5];
n=numel(x);
index=interp1([-realmax,x(2:end),realmax],[1:n,n],xp,'previous')
yp=x(index)

サインインしてコメントする。

Fangjun Jiang
Fangjun Jiang 2018 年 9 月 17 日
編集済み: Fangjun Jiang 2018 年 9 月 17 日
Now I think your problem has nothing to do with the increasing or decreasing of the values in x. Rather, it is to lookup and "match" the values in x towards zero, similar to the difference between floor(), ceil() and fix(). To do this, use the 'previous' and 'next' option of interp1() based on the sign of xp value. Please note, you shall add -realmax and realmax to avoid extrapolation, add 0 to avoid nondeterministic when xp fells between two points in x that are on the opposite side of zero.
x=[1 1.2 1.4 1.6 1.8 2 -1 -1.2 -1.4 -1.6 -1.8 -2];
xp=[-1.3, -2.5, -0.5 1 1.3, 2.5, 0.5];
xMod=[x,-realmax,0,realmax];
yp=zeros(size(xp));
for k=1:numel(xp)
if xp(k)>=0
yp(k)=interp1(xMod,xMod,xp(k),'previous');
else
yp(k)=interp1(xMod,xMod,xp(k),'next');
end
end

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2018 年 9 月 13 日

編集済み:

2018 年 9 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by