First off, this question is related to several other meta posts, but as far as I can tell none of the questions cover it. First a list of similar questions and why they are different:
- Is it appropriate to edit excessive line breaks from code? (1)
- Should you edit someone else's code in a question? (2)
- Suggested edit attempts to format code (3)
- Edit dispute - Indentation on questions (4)
Why this question is different:
- This question is not about the code itself, but the console output
- This question is not about the readability of the code, but the output of the code
- Again, different question (see the edit in question here)
- Again, different question (the edit is not about the code itself).
The post in question is: Horizontally concatenating the entries of two matrices, and padding with NaNs. In the code block, the first lines are variable assignments, such as: a=[1 NaN 3 0; 1 0 0 0]
. On SO, I would have added a space a = [..]
, but that's obviously not OK here, however, the output from that line looks like this in the post:
>> a=[1 NaN 3 0; 1 0 0 0]
a =
1 NaN 3 0
1 0 0 0
The output in Matlab looks a lot cleaner, so the formatting in the post is due to some bad copy-pasting. This is what the output looks like:
>> a=[1 NaN 3 0; 1 0 0 0]
a =
1 NaN 3 0
1 0 0 0
Using format compact
, it will look like the block below. Note that format compact
doesn't in any way alter the code.
a=[1 NaN 3 0; 1 0 0 0]
a =
1 NaN 3 0
1 0 0 0
I think people might reject the edit and say format compact
alters the code and should be included in a review instead. I disagree, but I won't take that fight. I do however, as it doesn't alter the code in any way, want to make the following edit, preferably the second alternative, but the first one would be acceptable too:
Alternative 1: Click here to see screenshot from Matlab
>> a=[1 NaN 3 0; 1 0 0 0]
a =
1 NaN 3 0
1 0 0 0
>> b=[1 0 1 0; 0 0 0 0]
b =
1 0 1 0
0 0 0 0
>> c=NaN(2,8) %same dimension as a and b
c =
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
for ii=1:size(a,1)
[v,x]=find(a(ii,:));
if numel(x)>0 %it could be all null
x1=a(ii,1:x(end)); %x(end) because I want to retain the zeros before the last not-null element
end
[v,x]=find(b(ii,:));
if numel(x)>0
x1=[x1 b(ii,1:x(end))];
end
c(ii,1:numel(x1))=x1;
end
>> c
c =
1 NaN 3 1 0 1 NaN NaN
1 NaN NaN NaN NaN NaN NaN NaN
Alternative 2, using format compact
: Click here to see screenshot from Matlab
a=[1 NaN 3 0; 1 0 0 0]
a =
1 NaN 3 0
1 0 0 0
b=[1 0 1 0; 0 0 0 0]
b =
1 0 1 0
0 0 0 0
c=NaN(2,8) %same dimension as a and b
c =
NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN
for ii=1:size(a,1)
[v,x]=find(a(ii,:));
if numel(x)>0 %it could be all null
x1=a(ii,1:x(end)); %x(end) because I want to retain the zeros before the last not-null element
end
[v,x]=find(b(ii,:));
if numel(x)>0
x1=[x1 b(ii,1:x(end))];
end
c(ii,1:numel(x1))=x1;
end
c
c =
1 NaN 3 1 0 1 NaN NaN
1 NaN NaN NaN NaN NaN NaN NaN
Neither of these edits alters anything OP has written in Matlab, only the format on SO.
Can I make such an edit? Are both edits OK?