cell merge and print

1 Ansicht (letzte 30 Tage)
Esra Demirbilek
Esra Demirbilek am 15 Mai 2022
Kommentiert: Voss am 16 Mai 2022
Hi
I have a problem can you help me?
This is problem;
I have a matrix like below;
45 45 65 65 28 28
1 1 9 2 4 1
9 4 7 2 3 1
this is how i want to do;
45-45 65-65 28-28
1-1 9-2 4-1
9-4 7-2 3-1
So I want to merge both cells into one cell, how can I do it?
thanks advance

Akzeptierte Antwort

Voss
Voss am 15 Mai 2022
Maybe something like this?
M = [ ...
45 45 65 65 28 28
1 1 9 2 4 1
9 4 7 2 3 1
];
fprintf([repmat('%d-%d\t',1,size(M,2)/2) '\n'],M.')
45-45 65-65 28-28 1-1 9-2 4-1 9-4 7-2 3-1
  2 Kommentare
Esra Demirbilek
Esra Demirbilek am 16 Mai 2022
how can i print to workspace and i want to create a new matrix how can i do
Voss
Voss am 16 Mai 2022
This prints to the command window. Is that what you mean by "print to workspace"?
And what do you want the new matrix to be, for the matrix M given above?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 15 Mai 2022
Depends upon what you mean by "merge" here...if you want the numeric result, that's
D=-reshape(diff(reshape(M.',2,[])),size(M,2)/2,[]).';
on above sample M array gives
>> D =
0 0 0
0 7 3
5 5 2
>>
NB: The negation operation since diff is X(i+1)-X(i) and the result of the first reshape() to create the 2-row array places the originally even-numbered column as the second row so it's the opposite direction of the way in which you've defined the differences. One could flipud the transpose before diff instead, but simply taking the negative is probably quicker.
If you mean you want to keep the two values but show them in a cell array as a 2-vector, that's for mat2cell() but one presumes that's not the actual Q?

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by