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])
Related
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.
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 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.
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
I have wrote a small program to test some funnction.
This is the proram:
close all;
clear all;
f = #(x, n) power(-1,(n-1)./2) .* power(x, n) ./ factorial(n);
n = [0,3,5,10,50,100];
% n = 0:10:100;
x = linspace(0, 4*pi, 1000);
ax = axes('nextplot', 'add');
for k = 1:length(n)
plot(ax, x, f(x, n(k)), 'displayname', ['f_', int2str(n(k)), '(x)']);
end
The main problem for me is that I thought that colon notation(1:10) definies array with values equaliy spread.
In my program that is not the case.
There is difference in output of the program when I set n as [0,3,5,10,50,100] and if I set n as 0:10:100.
In the first version, with array, the program works fine, but wit the second version, with colon notation, the program does not work it simply draws a line at 0.
So my question is way this is happening? I mean if the colon notation and the array definition are the same why does the program has different output for the colon notation and array notation?
Did I missunderstod something?
Thanks!!
EDIT:
This are the plots that I get:
First id array notation, second is colon notation.
I am using mathlab R2013a
In your commented line,
% n=[0:10:100];
you create a vector with values from 0 to 100, with spacing 10, ie [0 10 20 30 40 50 60 70 80 90 100].
With your uncommented line,
n=[0 3 5 10 50 100];
you have the values you specify.
Since they're not the same input, you won't get identical output.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I retrieve the last element of a vector only, provided that the length of that vector is unknown?
Use the special end keyword:
lastelement = myvector(end);
If the vector is called A, just use A(end).
In this case, use end, like #nispio and #David answered.
But it seems you think that not knowing the length can be a problem, but nope. That's because you can use length(v) if v is a column or row vector, or size(M) if M is a matrix.
Then, to get the last element of your vector, you could use (not recommended):
v(length(v)) if v is a row or a column vector
v(size(v,1)) if v is a column vector
v(size(v,2)) if v is a row vector
But if you use one of them, MATLAB will warn you:
The operation or expression <Indexing> has no evident effect.