MATLAB Answers

Integrate new rows inside matrix and repeating them

1 view (last 30 days)
I want to integrate two lines inside matrix and repeated frequently before each rows:
The original matrix:
A = [ G1 X8 Y118
G1 X8 Y135
G1 X 8 Y 150
G1 X 8 Y 172
G1 X 8 Y 209
G1 X 8 Y 242
G1 X 8 Y 410
G1 X 9 Y 208
…….. ] so on
Generated matrix
AA = [
M 802
M 800 P 8
G1 X8 Y118
M802
M800 P8
G1 X8 Y135
M 802
M 800 P 8
G1 X 8 Y 150
M 802
M 800 P 8
G1 X 8 Y 172
M 802
M 800 P 8
G1 X 8 Y 209
M 802
M 800 P 8
G1 X 8 Y 242
M 802
M 800 P 8
G1 X 8 Y 410
M 802
M 800 P 8
G1 X 9 Y 208
…………] so on
So before any row, put these two rows
M802
M800 P8
How can we do this?
  2 Comments
Islam Hassan
Islam Hassan on 19 Jul 2020
All rows must have same numbers, we can define the repeated raws like this for example:
Repeat1 = ["M"+802,"","";"M"+800, "P"+8,""];
M, P, G1 just letters.
I tagged image processing becasue this matrix generated after doing image processing of a figure then I want to put the output matrix in a certian format (like G-code). May be I misjuddge this tag.

Sign in to comment.

Accepted Answer

Devineni Aslesha
Devineni Aslesha on 21 Jul 2020
Here is the code to integrate two lines inside matrix and repeated frequently before each rows.
A = [ "G"+1 "X"+8 "Y"+118
"G"+1 "X"+8 "Y"+135
"G"+1 "X"+8 "Y"+150
"G"+1 "X"+8 "Y"+170
"G"+1 "X"+8 "Y"+209];
Repeat1 = ["M"+802,"","";"M"+800, "P"+8,""];
rows = size(A,1);
AA = [];
for i = 1:rows
AA = [AA; vertcat(Repeat1, A(i,:))]
end
For more information, refer the following link
  2 Comments
Devineni Aslesha
Devineni Aslesha on 22 Jul 2020
Hi Islam,
With the help of the code given in the answer, you can make use of if and else statements in the for loop to add the specified rows.
For more information, refer the following links,

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!