Error using ones Size inputs must be integers.

Hi guys i have a problem with my code for error like on theme " Error using ones Size inputs must be integers. Error in m6 (line 6) K1 = ones(length(strings)/4,1);"
my data looks like (look on image) and my code (strings is my Data) :
K1 = ones(length(strings)/4,1);
K2 = ones(length(strings)/4,1);
K3 = ones(length(strings)/4,1);
j=1;
for i = 2:length(strings)
WK11 = corrcoef((str2double(strings(2:i,2))), (str2double(strings(2:i,4))));
if rem(i,4)==0
K1(j,1) = WK11(2,1);
j=j+1;
end
end
j=1;
for i = 2:length(strings)
WK22 = corrcoef((str2double(strings(2:i,2))), (str2double(strings(2:i,3))));
if rem(i,4)==0
K2(j,1) = WK22(2,1);
j=j+1;
end
end;
j=1;
for i = 2:length(strings)
WK33 = corrcoef((str2double(strings(2:i,3))), (str2double(strings(2:i,4))));
if rem(i,4)==0
K3(j,1) = WK33(2,1);
j=j+1;
end
end;
I dont know how to solve this

댓글 수: 1

Guillaume
Guillaume 2018년 1월 5일
@Themiya, I've deleted your answer because a) it is not formatted properly (use the {}Code buton), b) it does not even appear to answer the OP question, and c) it is a duplicate of your comment.

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

답변 (3개)

Star Strider
Star Strider 2017년 12월 3일

1 개 추천

To force them to be integers, use fix, floor, ceil, or round.
Example
K1 = ones(fix(length(strings)/4),1);
Be careful with the length, numel, and size functions, so you get the result you want.

댓글 수: 3

Guillaume
Guillaume 2018년 1월 5일
Be careful with the length, numel, and size functions
Of the 3, length is the only one that could cause problem, so a simple rule is to never use it. numel or size with an explicit dimension is always safer.
I certainly agree that size with a specific dimension is best. For a vector, numel works, but for a matrix will not give the desired result, and length of course returns the greatest dimension. (I should have mentioned these details originally.)
Adam
Adam 2018년 1월 5일
numel 'works' for a matrix too depending what the 'desired result' is. I've used it numerous times to give me the total number of elements in the array. To be fair, I only started doing this though since Matlab suggested it as an improvement on my use of prod and size!

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

Image Analyst
Image Analyst 2018년 1월 5일

0 개 추천

strings is a cell array that does not have row (elements) that are an integer multipl of 4 so length(strings)/4 is not an integer. Rather than round that number I think it's best to track down the root cause of this, which is why strings is not a multiple of 4 when you think it always should be.
Hector Molina
Hector Molina 2019년 10월 30일

0 개 추천

I had a similiar problem,
as "ones" must to be integer, what I used as a solution, was the function "fix"
I can notice that you are dividing by 4 in your script, so try
ones(fix(length(strings)/4,1))
of course you will lose or gain information when apply "fix", however, it is posible your script (program) runs OK.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2017년 12월 3일

답변:

2019년 10월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by