# Transforming a row (inside a matrix) with equal numbers into a row with only one number

1 visualizzazione (ultimi 30 giorni)
Alberto Acri il 1 Nov 2023
Modificato: Dyuman Joshi il 1 Nov 2023
I have amatrix of the following type:
in = [228 228 228
231 231 231
183 183 183
157 157 157
168 168 168];
I need to know if the 3 columns of each row are equal and, if they are, get an Rx1 matrix at the end:
out = [228; 231; 183; 157; 168];
If they are not, keep the initial matrix Rx3 ('in' matrix) and tell me in which row the values of the three columns are not the same.
in_new = [228 228 228
231 231 231
183 200 183
157 157 157
168 168 200];
out_new = [3;5];
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Dyuman Joshi il 1 Nov 2023
Modificato: Dyuman Joshi il 1 Nov 2023
in1 = [228 228 228
231 231 231
183 183 183
157 157 157
168 168 168];
[a1, b1] = transform(in1)
a1 = 5×1
228 231 183 157 168
b1 = []
in2 = [228 228 228
231 231 231
183 200 183
157 157 157
168 168 200];
[a2, b2] = transform(in2)
a2 = 5×3
228 228 228 231 231 231 183 200 183 157 157 157 168 168 200
b2 = 2×1
3 5
function [out, out_new] = transform(in)
idx = all(~diff(in, [], 2), 2);
if all(idx)
out = in(:,1);
out_new = [];
else
out = in;
out_new = find(~idx);
end
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by