Table printing null values until it satisfies the if condition
4 次查看(过去 30 天)
显示 更早的评论
Hello. I'm new to matlab. I'm trying to create a table based on an if condition. But the output table is printing null values until it reaches the row where it satifies the condition. So my table basically has a number of null rows before listing the actual rows I wanted. I think I must use an else block to skip if it doesn't meet my if condition but I don't know how to do that. I also came across something called logical indexing "output_table(condition, column) = value;" but I don't know how to use it. Please help. Thanks in advance.
if (type(j) == 4 && TypeProb (j) >= 25)
count = count+1;
box = i;
Var1(j,:)=type(j);
Var2(j,:)=dx(j);
Var3(j,:)=dy(j);
Var4(j,:)=vehicle_vx(j);
Var5(j,:)=vehicle_vy(j);
Var6(j,:)=vehicle_poslat(j);
Var7(j,:)=vehicle_poslong(j);
Var8(j,:)=timestamp(j);
Var9(j,:)=box;
Var10(j,:)=TypeProb(j);
T1 = table(Var1,Var2,Var3,Var4,Var5,Var6,Var7,Var8,Var9,Var10,'VariableNames',Varname);
end
writetable(T1,'QW.xls');
0 个评论
采纳的回答
Ritish Sehgal
2023-1-27
Hi,
I assume that you are looping over the if-else statements to create a table based on a condition.
You can create an else block and use the ‘continue’ command to skip the iteration in order to avoid writing null values to your table.
Here is the documentation for the ‘continue’ command.
Regarding logical indexing, it is a way to access or assign values to a matrix using a logical array with the same dimensions.
For example:
Suppose you have a matrix M and a logical array L of the same size, you can use the following syntax:
M(L) = value;
The above statement will assign the value to all the elements of M where the corresponding element in L is true.
Logical indexing can also be used to access elements of a matrix.
For example:
P = M(L);
The above statement would return a new matrix P that contains only the elements of M where the corresponding element in L is true.
You can refer to the documentation for examples of logical indexing.
Hope it helps!!
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!