how to assign 1 to negative value and 0 to positive values in dataset

i have data in excel consist of 404 rows and 19 columns. In data there are some negative values and positive values so i have to assign '1' to all values in data which is less than zero and '0' to all values which is more than or equal to zero.

답변 (1개)

n = xlsread('name_your_xlsx_file.xlsx');
out = n < 0;

댓글 수: 6

sorry sir..but not working..i want to assign '1' to all negative values and '0' to all positive values including 0
James Tursa
James Tursa 2016년 2월 4일
편집: James Tursa 2016년 2월 4일
Why don't you think it is working? What code are you running and what results are you getting that don't seem correct?
for (i=1;i<57;i++)
{
for (j=1;j<403;j++)
{
if(value<0)
cell value= 1;
else
cell value =0;
endif
}
}
this is my pseudo code written in C. i want to convert this logic in Matlab but don't know how. first i want read my xlsx sheet and then want to apply this logic so that my data matrix contains only 0 and 1 values.
Did you even try Andrei's answer? It is exactly equivalent to your pseudo C++ code. See the doc. Note that it is a very basic matlab concept.
The one minor difference between the pseudocode and Andrei's code is that the pseudocode assigns a 1 and 0 of some unknown data type, whereas Andrei's code assigns 1 and 0 of type logical . logical values and numeric values are transparently converted for most purposes, but from time to time you might need to specifically convert logical values to some other data type such as by using out = double(n<0) if you need double precision 0 and double precision 1 as the values.

댓글을 달려면 로그인하십시오.

카테고리

질문:

2016년 2월 3일

댓글:

2016년 2월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by