10
\$\begingroup\$

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:

Why this question is different:

  1. This question is not about the code itself, but the console output
  2. This question is not about the readability of the code, but the output of the code
  3. Again, different question (see the edit in question here)
  4. 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?

\$\endgroup\$
  • \$\begingroup\$ While I'd love to agree, in principal, changing the result of code could make it look like the code that people are writing is not producing the desired output... which is off-topic. This might not be the case, especially if whitespace isn't necessarily part of the result.... but still \$\endgroup\$ – Quill Jul 16 '16 at 14:42
  • 1
    \$\begingroup\$ I'm 100% sure that it's impossible to get such an output from Matlab unless you're doing some very obscure undocumented changes in the editor settings. \$\endgroup\$ – Stewie Griffin Jul 16 '16 at 14:44
  • \$\begingroup\$ Matlab doesn't have strings? And even if that were true, language specific rules aren't good :( \$\endgroup\$ – Quill Jul 16 '16 at 14:46
5
\$\begingroup\$

If you're absolutely sure it's a copy-paste error (because you've reproduced the output, for example), feel free to fix it.

If there's any doubt whatsoever, don't.

It's very important the output provided by the author is shown exactly as it is produced.

\$\endgroup\$
  • \$\begingroup\$ I don't think it's a copy paste error, so much as that the whitespace in the output could look better formatted \$\endgroup\$ – Quill Jul 16 '16 at 14:47
  • \$\begingroup\$ @Quill Looks like how it looks in MatLab is not the same as how the author posted it. I'd consider this a copy-paste error. \$\endgroup\$ – Mast Jul 16 '16 at 14:56
3
\$\begingroup\$

Yes, absolutely!

In this case, it is not the author's code doing that output, but Matlab.

In this specific case the output is just to show the values of the variables, and I'd also say that edits to show that in a different way (for example by separating it from the code block into something else) would be okay, as you are not editing the code that is meant to be reviewed, but the extra context that is given.

My general rule would be:

If it's not the author's code responsible for the output formatting, but an IDE, feel free to edit.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .