Table printing null values until it satisfies the if condition

4 vues (au cours des 30 derniers jours)
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');

Réponse acceptée

Ritish Sehgal
Ritish Sehgal le 27 Jan 2023
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!!

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by