Hi guys,
I wanted to assign increment rows of null to a variable "C". The variable C has a size of 138x6x69. My code is shown below.
a = 1;
for i = 1:69
C(1:a,:,i)=[];
a = a+2;
end
But the error was " A null assignment can have only one non-colon index.A null assignment can have only one non-colon index."
Can anyone teach me another way of modifying the code? I really need help.
Much Thanks
Ben

 Réponse acceptée

Raj
Raj le 20 Sep 2019

2 votes

You cannot assign 'null' like this to a 3D matrix while wanting to maintain the dimensions of the matrix. You can assign zero or NaN depending on what you want to achieve here.

7 commentaires

Benedict Teng
Benedict Teng le 20 Sep 2019
My objective is to remove 1st row in the first 138x6 of C to make it 137x6, then delete 3 rows in the second of C, then delete 5 rows in the third of C and continues on to the 69th of C. Can you suggest any way to code that?
Raj
Raj le 20 Sep 2019
"remove 1st row in the first 138x6 of C to make it 137x6, then delete 3 rows in the second of C, then delete 5 rows in the third of C and continues on to the 69th of C" - See that's the point. You cannot combine these different dimensions individual 2D matrices to make a single 3D martix of one size. You can create separate 2D matrices though but I am assuming that's not what you want.
Stephen23
Stephen23 le 20 Sep 2019
"My objective is to remove 1st row in the first 138x6 of C to make it 137x6, then delete 3 rows in the second of C, then delete 5 rows in the third of C and continues on to the 69th of C"
This is not possible: if you remove rows from an array then they will be removed from all columns/pages/higher dimensions. It is not possible to create a "ragged array".
Benedict Teng
Benedict Teng le 20 Sep 2019
I've thought of another way that is to connect all of them into a 2D matrix "B" into size 9522x6, and I wanted to do the same thing:remove 1 row in the first 140 lines, remove 3 rows in the next 140 lines, 5 rows in the next 140 lines and so on.
I am thinking to use a for loop but it seems like the matrix size change every time after I assign null the rows, and it makes the code more complicated.
Can you suggest any better way to do this?
Raj
Raj le 20 Sep 2019
Modifié(e) : Raj le 20 Sep 2019
"matrix size change every time after I assign null the rows, and it makes the code more complicated" - Yes that is expected. Matrix dimension will change everytime you assign null to a row because you are effectively removing that row. You can do something like this to avoid this complication: Instead of assigning 'null', assign zero and then from the final matrix remove all rows which are all zeros.
Stephen23
Stephen23 le 20 Sep 2019
Modifié(e) : Stephen23 le 20 Sep 2019
"but it seems like the matrix size change every time after I assign null the rows"
Because what you are doing is removing rows:
MATLAB does not have a "null" concept, even though you keep using this term. You can't "assign null" as you write, because "null" does not exist.
Can you suggest any better way to do this?
Collect all of the required indices into a vector, use it to remove all rows at once after the loop.
Benedict Teng
Benedict Teng le 20 Sep 2019
I've got it, thanks for the advices guys. Really appreciate it.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by