Array type effected by variable type
조회 수: 1 (최근 30일)
이전 댓글 표시
x=uint64(5);
c=[-2,x];
c becomes [0,5] because c = uint64 array.
So I dont want it to be effected by input types. I want c=[-2,5]
how do I do that?
댓글 수: 1
Dyuman Joshi
2023년 9월 4일
편집: Dyuman Joshi
2023년 9월 4일
Numeric arrays (in MATLAB) are always homogenous. When you concatenate values of multiple (2 or more) numeric data types, the final data type would be the one of the left most integer (If present) or single (if only single and double are present)
"So I dont want it to be effected by input types."
Things don't work that way in MATLAB.
"how do I do that?"
Instead of unsigned integers, use signed integers or double or single data types. Take into consideration the difference of ranges and other specifications of each data type.
답변 (1개)
Riya
2023년 9월 7일
Hello Melih Furkan SEN,
As per my understanding, you don’t want that array type is affected by variable type.
Please note that, if you want to create an array `c` with the values [-2, 5] regardless of the input type of `x`, you can convert `x` to a double before creating the array. Here's how you can achieve that:
x = uint64(5);
c = [-2, double(x)];
By using the `double` function, you can convert `x` to a double precision floating-point number, which allows it to be combined with other double values in the array `c`. This way, the resulting array `c` will have the desired values [-2, 5] without being affected by the input type of `x`.
You can refer the following documentations to know more:
- Combining Integer and Non-Integer type data : https://www.mathworks.com/help/matlab/matlab_prog/combining-integer-and-noninteger-data.html
- Concatenation Examples : https://www.mathworks.com/help/matlab/matlab_prog/concatenation-examples.html
- Combining Unlike Integer Types : https://www.mathworks.com/help/matlab/matlab_prog/combining-unlike-integer-types.html
I hope it helps!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!