Integrate new rows inside matrix and repeating them

조회 수: 1 (최근 30일)
Islam Hassan
Islam Hassan 2020년 7월 19일
댓글: Devineni Aslesha 2020년 7월 22일
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
Image Analyst
Image Analyst 2020년 7월 19일
편집: Image Analyst 2020년 7월 19일
Are M, P, G1, etc. all variables of double class? Your AA matrix can't have some rows with two columns, like M and 802, and other rows with 3 columns, like [G1, X8, Y118] or 4 columns like [M, 800, P, 8]. Matrices have to be rectangular, meaning no ragged right edge. All rows must have the same number of columns.
And you tagged this image processing. What does this have to do with image processing???
Islam Hassan
Islam Hassan 2020년 7월 19일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Devineni Aslesha
Devineni Aslesha 2020년 7월 21일
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
Islam Hassan
Islam Hassan 2020년 7월 21일
Thanks a lot for your answer. That will solve it.
However, I have a related question, if I cave matrix like this:
A = [ M 802
M 800 P 8
G1 X8 Y118
M802
M800 P9
G0 X8 Y135
M 802
M 800 P 8
G1 X 8 Y 150
M 802
M 800 P 9
G0 X 8 Y 172
M 802
M 800 P 8
G1 X 9 Y 209
M 802
M 800 P 9
G0 X 9 Y 242
M 802
M 800 P 8
G1 X 10 Y 410
M 802
M 800 P 9
G0 X 10 Y 208
…………] so on
I want add these two different rows, to creatw these;
AA = [ M 802
M 800 P 8
G1 X8 Y4
M802
M800 P9
G0 X8 Y5
M 802
M 800 P 8
G1 X 8 Y 6
M 802
M 800 P 9
G0 X 8 Y 7
M 802
M 800 P 8
G1 X 8 Y 10
G1 X 9 Y 10
G1 X 9 Y 9
M 802
M 800 P 9
G0 X 9 Y 6
M 802
M 800 P 8
G1 X 9 Y 0
G1 X 10 Y 0
G1 X 10 Y 410
M 802
M 800 P 9
G0 X 10 Y 208
G1 X 10 Y 10
G1 X 11 Y 10
…………] so on
add these rows G1 X 8 Y 10 ; G1 X 9 Y 10 ; depending on the last "X"+ value, if the value is even use G1 X "equal to last value" Y 10 ; G1 X "value +1" Y 10.
and if it is odd use G1 X "equal to last value" Y 0 ; G1 X "value +1" Y 0.
Thanks in advance.
Devineni Aslesha
Devineni Aslesha 2020년 7월 22일
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,

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by