0

I have a 4x800 data. I need to find the max and min value by column and based on the result. I have to change the max value to 1 and the min value to 0. This is my code:

M = A==repmat(max(A),size(A,1),1);
M( cumsum(M)>1 ) = 0;
filename = 'result10.xlsx'; 
xlswrite(filename,M);

As you can see, I am also writing the results to an Excel file. However, when I open up the file in Excel, it shows true and false values. How do I make the values become 1 and 0?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • Why would you want to copy and paste the result into Excel? Can't you just use `xlswrite`? – rayryeng Nov 27 '14 at 18:58
  • i just learnt how to use matlab. and ive use the xlswrite function. but the value would still be true and false instead of 1 and 0 in the xlsx doc. @rayryeng – user3103158 Nov 27 '14 at 19:12
  • can you show your whole code instead of us just guessing what you did? Thanks! – Benoit_11 Nov 27 '14 at 19:17
  • 1
    this is my code : M = A==repmat(max(A),size(A,1),1); M( cumsum(M)>1 ) = 0; filename = 'result10.xlsx'; xlswrite(filename,M); the variable A containg 4x800 data of numbers. @Benoit_11 – user3103158 Nov 27 '14 at 19:20
  • bah. That's because `M` is LOGICAL. That's why it's writing `true/false` to your XLS file. If you want the actual numerical `0/1`, you need to cast your matrix to `double` before writing. I have provided an answer. – rayryeng Nov 27 '14 at 19:21
  • Are you sure this code does what you want it to? It doesn't seem to fit your description of it. Anyway, rayryeng is right, you have to cast the matrix to double, uint8, ... or something similar. – Stewie Griffin Nov 27 '14 at 19:24
  • @RobertP. - I agree. I don't think the code described in the post is agreeing with the description in the question. Either way, the OP seems to know what they're talking about, so I'm not questioning their code logic. – rayryeng Nov 27 '14 at 19:25
  • @user3103158 - You're welcome. If I helped, and if this has solved your question, consider accepting my answer. Good luck! – rayryeng Nov 27 '14 at 19:29

1 Answers1

3

The reason why it is writing true/false to your XLS file is because M is a logical array. To make this 0/1 cast your matrix to double before writing to your XLS file. Therefore:

M = A==repmat(max(A),size(A,1),1); 
M( cumsum(M)>1 ) = 0; 
M = double(M); %// CHANGE IS HERE
filename = 'result10.xlsx'; 
xlswrite(filename,M);

The above will make your values floating point. If you want them to be integer, cast them to some integer type, like int32, uint8, etc. (tip of the hat goes to Robert P. for this small tip!)

rayryeng
  • 102,964
  • 22
  • 184
  • 193