How to compare two variables data by using ifelse? if both variable contain same number and length of data, so condition becomes true it should perform desired functionality.

23 ビュー (過去 30 日間)
i want to establish a logic that if variable data is same as variable data "a" or "b'' or "c" or "d"then it should perform the listed functionality
i was trying the below code to get second elseif true and answer "Value==2" but when it first compare if data == a... it gives error >>>Matrix Dimension Must Agree"
a = -6:6;
b = -3:3;
c = 0:6;
d = 0:12;
% data = [-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6] % user selected
data = [-3 -2 -1 0 1 2 3] % For this Data condition "Value = 2" should be answer
% data = [0 1 2 3 4 5 6]
% data = [0 1 2 3 4 5 6 7 8 9 10 11 12]
if data == a
value =1
elseif data == b
value = 2
elseif data == c
value = 3
elseif data == d
value = 4
end
it is giving error that "Matrix Dimesions Must Agree"
  2 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 10 月 19 日
If you only plot when 'b' and 'c' are equal to 'a', then are you trying to plot(a, a).
taimour sadiq
taimour sadiq 2020 年 10 月 19 日
i apalojize i edited my question.. can u please review...
i want that variable "data" should b compared with variable a,b,c,d and see which comparison satisfy the condition

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

採用された回答

KSSV
KSSV 2020 年 10 月 19 日
Read about isequal.
a = -6:6;
b = -3:3;
c = 0:6;
d = 0:12;
% data = [-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6] % user selected
data = [-3 -2 -1 0 1 2 3] % For this Data condition "Value = 2" should be answer
% data = [0 1 2 3 4 5 6]
% data = [0 1 2 3 4 5 6 7 8 9 10 11 12]
if isequal(data,a)
value =1
elseif isequal(data,b)
value = 2
elseif isequal(data,c)
value = 3
elseif isequal(data,d)
value = 4
end
But what you are trying to do.....what you are doing is not fine.

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 10 月 19 日
編集済み: Ameer Hamza 2020 年 10 月 19 日
data and a,b,c,d have different dimensions. Use isequal()
if isequal(data, a)
value =1
elseif isequal(data, b)
value = 2
elseif isequal(data, c)
value = 3
elseif isequal(data, d)
value = 4
end
Or the following more general approach
a = -6:6;
b = -3:3;
c = 0:6;
d = 0:12;
data = [-3 -2 -1 0 1 2 3]; % For this Data condition "Value = 2" should be answer
C = {a, b, c, d};
value = find(cellfun(@(x) isequal(data, x), C));

カテゴリ

Help Center および File ExchangeModel Verification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by