Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I have tried to create vector matrix array, to add integer elements after their arrival.
a={[1 1 1 1 1]; [3 3 3 3 3 3 3 3 3]; [4 4]; [5]};
print(a);
That code gives me this error:
You should take a look at the documentation for print. It is used to:
Print figure or save to specific file format
What you want is either disp which is used to
Display value of variable
Or fprintf which is used to:
Write data to text file (which can be the console)
Or even simpler: Just write
a % Note the absence of ';'
Upon encountering an operation without semicolon, MATLAB aromatically displays the result in the console. So this is enough to print you variable.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I understand how the program works but I have a little bit of confusion. If anybody can explain, that will be great. The output is 21, 12. Does it work like 7*3=21 and 4*3=12?
mat=[7 11 3; 3:5];
[r,c]=size(mat);
for i=1:r
fprintf ('The sum is %d\n',sum(mat(i,:)))
end
mat(i,:) will give you all values in the first row of mat. In your example, this first row is [7 11 3], and the second row is [3 4 5]. The outputs you're seeing are the sums of all values in each row (7+11+3=21).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to know what this line of code does.
ind_x = [1,3:5:size(paths,2)]
What would ind_x contain after this line? I already know that size(paths,2) means the size of second dimension of paths matrix.
3:5:size(path,2) returns a vector which starts from 3 to size(path,2) with steps 5. For example, if size(path,2) is equal to 20, the result would be:
ind_x
[1 3 8 13 18]
As you can see it counts from 3 to size(path,2) with step size 5 (3, 8, 13, ...).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I got a few Matlab code to play. But the answer is not correct:
x = linspace(-pi, pi, 10)
sinc = #(x) sin(x) ./ x
sinc(x) // wrong result occurs at here.
The expected result as below:
ans =
Columns 1 through 6:
3.8982e-17 2.6306e-01 5.6425e-01 8.2699e-01 9.7982e-01 9.7982e-01
Columns 7 through 10:
8.2699e-01 5.6425e-01 2.6306e-01 3.8982e-17
real result:
ans =
Columns 1 through 3
0.000000000000000 0.263064408273866 0.564253278793615
Columns 4 through 6
0.826993343132688 0.979815536051016 0.979815536051016
Columns 7 through 9
0.826993343132688 0.564253278793615 0.263064408273866
Column 10
0.000000000000000
details: My OS is arch linux,
Matlab is downloaded through official website.
matlab version is 2015b
The expected result and the real results you present are identical as far as I can see.
The only difference is the notation: normal vs scientific.
With format short you can switch to scientific notation and get identical results with identical formatting.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Here is the code.
>> a=ones(1,10);
>> b=size(a);
>> repmat(1,b)
ans =
1 1 1 1 1 1 1 1 1 1
>> repmat(1,(1,10))
repmat(1,(1,10))
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
Does anyone know why? and why does the error go like that? Thanks.
The expression size(a) returns [1 10], not (1,10). So the equivalent is:
repmat(1, [1 10]);
Here's some helpful documentation:
Matrices and Arrays
The size function
The repmat function
The full equivalent to your example code is repmat(1,size(ones(1,10))). Alternatively you can use a repmat(1,[1,10]), for array construction you have to use [], the round brackets () are for function call and indexing only.
Repmat is used to create an array with n repeating copies of the source array (A). Hence, the second argument is a single scalar defining the number of copies of A, the first argument.
You can use a vector for the second argument, but it defines how many copies the result has in different dimensions. Syntax would be: repmat(1,[1 10])
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When i try to display binary notations which start with a zero in the 1st bit position, matlab discards the zero and displays only the other 7 bits. How do I display the 1st position too?
ex: when i try to display "01101111", matlab displays it as "1101111", but I need the 1st bit position value also. Can some one please help.
In Matlab, to display the bit representation of a number you need to convert it into a string with dec2bin().
So, if you have x = 111, it's binary representation is:
dec2bin(111)
ans =
1101111
which retains only the significant bits. To force an 8-bit representation use:
dec2bin(111,8)
ans =
01101111
Note, how the result will be a string. If you want to retrieve bits in numeric format, then use bitget():
bitget(111,8:-1:1)
ans =
0 1 1 0 1 1 1 1
Basically, if your need is purely visual, use dec2bin2() otherwise for manipulating bits, use the bit-wise operations functions, which accept and return numeric types.