Comparing ones in a table

4 次查看(过去 30 天)
basma awad
basma awad 2021-10-13
编辑: Kevin Holly 2021-10-13
Hello,
I have 2 different 1x1 struct with 4 fields in matlab where each fields ccontains 5 different a 103x5 table. I which to compare colum 3 and 5 in each of these fields for both struc. Both colums contains 1's and 0's and i want to have a output a vector that has gives a output 1 when both elemet of these colum is 1. So lets say,
A= [0 1 1 0 1 1 1]
B= [1 1 0 0 0 1 1]
I want the new vector to be
Output = [0 1 0 0 0 1 1]
Is there a smatter way then using if statement for each field ?

回答(1 个)

Kevin Holly
Kevin Holly 2021-10-13
编辑:Kevin Holly 2021-10-13
Here is one way of doing it.
t = table;
t.column1 = randi([0 1],[7 1]);
t.column2 = randi([0 1],[7 1]);
t.column3 = [0 1 1 0 1 1 1];
t.column4 = randi([0 1],[7 1]);
t.column5 = [1 1 0 0 0 1 1];
s.table = t;
Output = s.table.column3 == s.table.column5 & s.table.column3 + s.table.column5 ~= 0
Output = 1×7 logical array
0 1 0 0 0 1 1

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by