0
votes

I want to make a matrix like this:

n rows x 2 columns

22.3 18.3
22.4 18.4
22.5 18.3
22.4 18.3
22.2 18.6

In this case I have 5 rows by 2 columns

But I need to append a new row dynamically, removing the first based in a given window I want.

e.g. I have a window of N elements in row, when I append the new row in the matrix, I must remove the first one.

In the above example, a window of 5 rows: If I need to input (append) the new row: 22.8 17.1 the row will become like this:

22.3 18.3
22.4 18.4
22.5 18.3
22.4 18.3
22.2 18.6
22.8 17.1

There are 6 rows, I need to remove the first one. After that the row becomes like this (5 rows):

22.4 18.4
22.5 18.3
22.4 18.3
22.2 18.6
22.8 17.1

My question: - how to append a new row in matlab? - how to remove a new row in matlab?

I saw that in matlab It's possible to handle matrix vectors and cells.

E.g I achieved What I want doing that:

messageArr = [];
messageArr{end+1} = 23.01
messageArr{end+1} = 23.02
messageArr = messageArr.';%'//
messageArr{end+1} = 23.03
messageArr{end+1} = 23.04
messageArr{end+1} = 23.04
messageArr{end+1} = 23.04
messageArr{end+1} = 23.04
messageArr{end+1} = 23.04
messageArr{end+1} = 23.04
messageArr{end+1} = 23.04
messageArr{end+1} = 23.04
messageArr{end+1} = 23.04
messageArr{end+1} = 23.04
messageArr2 = [];
messageArr2{end+1} = 17.01
messageArr2{end+1} = 17.02
messageArr2{end+1} = 17.03
messageArr2{end+1} = 17.04
messageArr2{end+1} = 17.04
messageArr2{end+1} = 17.04
messageArr2{end+1} = 17.04
messageArr2{end+1} = 17.04
messageArr2{end+1} = 17.04
messageArr2{end+1} = 17.04
messageArr2{end+1} = 17.04
messageArr2{end+1} = 17.04
messageArr2{end+1} = 17.04
messageArr2 = messageArr2.'%'//
Y = horzcat(messageArr, messageArr2)
Y = cell2mat(Y)

It gave me a 13x2 double matrix:

23,0100000000000    17,0100000000000
23,0200000000000    17,0200000000000
23,0300000000000    17,0300000000000
23,0400000000000    17,0400000000000
23,0400000000000    17,0400000000000
23,0400000000000    17,0400000000000
23,0400000000000    17,0400000000000
23,0400000000000    17,0400000000000
23,0400000000000    17,0400000000000
23,0400000000000    17,0400000000000
23,0400000000000    17,0400000000000
23,0400000000000    17,0400000000000
23,0400000000000    17,0400000000000

But I think this is not the right way to do it. I need a more simple way to just append, and remove the first row without the necessity of concate cells and doing transpose of cells.

1
Is there any particular reason why you're using a cell array instead of a matrix? See concatenation, you managed to find horzcat, why not try vertcat? I would start by putting together your full matrix, checking its size, and then delete the rows you don't want.excaza
What you show is not a matrix but a cell array. This two are fundamentally different in MATLAB. I think you want a matrix!Ander Biguri
Is it? I opened the matlab's window and just copied. It was like this 13x2 double I think it represents it as cell when you double click the variable it creates in the workspace.felipe
In MATLAB to get rid of a row you execute Y(rowIdx,:) = [] . The end keyword may also come in handy here Y = [Y(end-3:end,;) ; newRow] will give you a moving window of size 5Some Guy
BigMat = [BigMat ; NewRow] You need to read a basic MATLAB tutorial. You can start here: mathworks.com/help/matlab/math/…beaker

1 Answers

0
votes

This code will append a new row and remove the first row:

% create five rows of two-column data
d=[22.3 18.3; 22.4 18.4; 22.5 18.3; 22.4 18.3; 22.2 18.6]
% this is a new row of data
newrow = [22.8 17.1]
% drop the first row and append the new data at the end
d = [d(2:end,:); newrow]

It uses the [ ] concatentation operator.