I have problem with this code (i reduced it before posting)
epsilon=0.60; t=-3.0; n=11; E=+0.34; I=eye(n,n); eta=1.0e-10;
DB = gallery('tridiag',t*ones(1,n-1),epsilon*ones(1,n),t*ones(1,n-1));
ODB1 = sparse(1:n,1:n,t*heaviside((-1).^(2:n+1)));
ODB2 = sparse(1:n,1:n,t*heaviside((-1).^(1:n)));
a=(E*I-DB)\ODB2';b=(E*I-DB)\ODB1;
p0=(E*I-DB)\ODB1';q0=(E*I-DB)\ODB2;
%initial
p=(I-a*q0-b*p0)\(a*p0);q=(I-a*q0-b*p0)\(b*q0);
tmp1=p; tmp2=q;
while norm([p q]) > 1.0e-8
A=(1+eta*1i)*I-p*q-q*p; (1)
x=A\I; (2)
p=x*p*p; q=x*q*q; (3)
%p=((1+eta*1i)*I-p*q-q*p)\(p*p); (4)
%q=((1+eta*1i)*I-p*q-q*p)\(q*q); (5)
tmp1=tmp1+tmp2*p;
tmp2=tmp2*q;
end
Matlab shows an error on RCOND if i replace (1-3) lines by (4-5). So what's difference between them? Can i trust the result?
Your matrix p*p is:
ans =
39.2012 0 -28.3944 0 10.1201 0 10.6411 0 -28.4320 0 38.6426
16.3606 0 -11.9292 0 4.3186 0 4.4104 0 -11.9335 0 16.2645
-28.3944 0 20.9268 0 -7.6333 0 -7.6709 0 20.8517 0 -28.4320
-28.2897 0 20.6792 0 -7.5188 0 -7.6149 0 20.6748 0 -28.1980
10.1201 0 -7.6333 0 3.1358 0 2.5773 0 -7.6709 0 10.6411
32.6084 0 -23.8794 0 8.7457 0 8.7457 0 -23.8794 0 32.6084
10.6411 0 -7.6709 0 2.5773 0 3.1358 0 -7.6333 0 10.1201
-28.1980 0 20.6748 0 -7.6149 0 -7.5188 0 20.6792 0 -28.2897
-28.4320 0 20.8517 0 -7.6709 0 -7.6333 0 20.9268 0 -28.3944
16.2645 0 -11.9335 0 4.4104 0 4.3186 0 -11.9292 0 16.3606
38.6426 0 -28.4320 0 10.6411 0 10.1201 0 -28.3944 0 39.2012
on the first run. its reciprocal condition number (something like condition number but scaled to 1-0 range) , rcon(p*p) is equal to zero, meaning its unsolvable algebraically.
I do not know what your matrices mean, but you can not solve the system you proposed in your code with the values of this equation.
Related
This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Matlab Error: ()-indexing must appear last in an index expression
(1 answer)
Closed 5 years ago.
I'm trying to realise the following Octave command in MATLAB:
M = eye(x)(y,:);
x is just a number (in my example 10) and y is a vector (here 8x1):
y = [1 3 4 5 7 10 9 10];
The Octave command would generate:
M =
1 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
The ones are kept very near to the diagonal.
The nearest I came with MATLAB is with the following commands:
n = size(y,1);
Y = eye(n, x);
but it would generate something still different. If the difference between rows and columns gets bigger, it would be very different.
M =
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
How could I get the first matrix with MATLAB?
First you should find what this expression eye(x)(y,:) means. First create an identity matrix with the size of x by x, and then select rows with index in y. Therefore, the equivalent syntax would be:
E = eye(x);
M = E(y,:);
I have a binary image. I have several single pixels in images. Single pixels are white (1) and all of their neighborhoods are black (0). for example image below shows a single pixel (at center) and two pixels (at left-bottom):
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
1 1 0 0 0
How can I remove single pixels with morphological operations in Matlab?
I give you another option without loop, using a 2D convolution with conv2:
M = [0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
1 1 0 0 0]
C = [0 1 0
1 1 1
0 1 0]; % The matrice that is going to check if a `1` is alone or not.
%if you also want to consider the neibhbors on the diagonal choose:
%C = ones(3);
R = M.*conv2(M,C,'same')>1 %Check for the neighbors.
RESULT
R =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 0 0 0
Upon request by the OP, I'm converting my short comment into a reply:
Since you explicitly asked for morphological operations: bwmorph has a 'clean' option which is described as "Removes isolated pixels (individual 1s that are surrounded by 0s)" with an example close to yours. Have a look at the bwmorph documentation page.
As in your previous question, you can use bwboundaries:
if P is the binary image, than:
B = bwboundaries(P,8);
for k = 1:numel(B)
if size(B{k})<=2
P(B{k}(1,1),B{k}(1,2)) = 0;
end
end
So for the example above P becomes:
P =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 0 0 0
I want to create a matrix of size m-by-n where all elements in a column are 0 except one element which is 1. That one element must be at a random position.
eg.
[0 1 0 0 0
0 0 1 0 0
1 0 0 1 0
0 0 0 0 0
0 0 0 0 1]
To add some variety, here's another approach:
m = 4;
n = 5;
[~, result] = sort(rand(m,n));
result = double(result==1);
This gives, for example,
result =
0 0 0 0 1
0 1 0 0 0
1 0 0 1 0
0 0 1 0 0
You can also use rand and max to do the job:
m=4;
n=5;
R=rand(m,n);
result = bsxfun(#eq, R, max(R,[],1))
On my machine it gave:
1 1 0 0 0
0 0 0 0 0
0 0 1 0 1
0 0 0 1 0
How it works: Generating a random matrix, R, and then setting to 1 the entry corresponding to the maximal element at each column. No need for sorting.
Regarding the original answer of Divakar, since it uses randperm it is restricted to square matrix only, and it will only produce random permutation matrices.
One possible way to correct his solution is to use randi instead of randperm:
result = bsxfun( #eq, (1:m)', randi(m, 1, n ) )
May give this output:
1 0 1 0 0
0 0 0 0 0
0 0 0 0 0
0 1 0 1 1
As for the answer of bla, using accumarry can save the use of zeros and sub2ind:
m=5; n=10;
R=randi(m,n,1);
A = accumarray( {R, (1:n)' }, 1, [m n] )
May give this output:
0 0 0 0 1 0 0 1 0 0
0 1 0 0 0 0 1 0 1 0
1 0 0 1 0 0 0 0 0 1
0 0 0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0 0 0
Another idea I have is to create the identity matrix of size m x m, then use randi with a range from 1 up to m to create a vector of n elements long. After, you'd use this vector to access the columns of the identity matrix to complete the random matrix you desire:
m = 5; n = 5; %// Given your example
M = eye(m);
out = M(:,randi(m, n, 1));
Here's one possible run of the above code:
out =
1 0 0 0 0
0 0 0 0 0
0 0 0 1 0
0 0 0 0 0
0 1 1 0 1
here's an example using randi:
m=5; n=10;
A=zeros(m,n);
R=randi(m,n,1);
A(sub2ind(size(A),R',1:n))=1
A =
0 0 0 0 0 0 0 1 0 1
0 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0
1 0 0 0 0 0 1 0 1 0
You can use sparse with randi for a one-liner, like so -
full(sparse(randi(m,1,n),1:n,1,m,n))
Sample run -
>> m = 5; n = 6;
>> full(sparse(randi(m,1,n),1:n,1,m,n))
ans =
0 1 0 0 0 1
0 0 1 1 0 0
0 0 0 0 0 0
1 0 0 0 1 0
0 0 0 0 0 0
I need to calculation R*S*R'.
R is an ordinary matrix.
But S is composed of values. The element of S is the value of F(w), and is calculated by
[PressureSpecAuto,F] = periodogram(....);
S{i,j} = PressureSpecAuto;
which means each element is a set of data.
The problem is that, Matlab cannot multiply cell matrix with matrix, then How can I solve this problem?
Notice: The element of S should not be treated as an vector. It's just the value set of function F(w).
UPDATE1:
Element in S(the value set of a function)
Essentially, element in S is a function's value, e.g. f(x). When multiplying, it is still R(1,:)*S(:,1). That is, R(1,1) * S(1,1) + R(1,2) * S(2,1) ...
UPDATE2:
R:
1 0 0 0 0 0
0 0 0 0 0 0
1 0 0 0 0 0
0 0 0 0 0 0
1 0 0 0 0 0
0 0 0 0 0 0
1 0 0 0 0 0
0 0 0 0 0 0
0 1 0 0 0 0
0 0 0 0 0 0
0 1 0 0 0 0
0 0 0 0 0 0
0 1 0 0 0 0
0 0 0 0 0 0
0 1 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 1 0 0
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 1 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 0 0 0
0 0 0 0 0 1
Element in S( e.g. S(1,1) ):
2.11586339015690e-23
6548.06822760155
10933.4416318101
67974.4878764171
1640.90694018577
22254.1105594943
54583.8668300499
25426.8190829386
4646.70203854458
19224.2485418923
17292.0278726986
928.765041030392
14728.5614115324
113385.034815149
30274.0332077125
22697.8886043178
61916.4030808219
38648.2740539840
127.547928632502
24452.0499691112
12311.1687443994
6627.23433956309
11264.7956369618
7232.97125504007
4120.08127891675
1546.69594235781
22795.2322822644
627.572461904325
9237.43533412019
3433.67898348596
Could you use a loop? Maybe this would work then...
Just use a loop and loop through the indices of S to extract each matrix. Then do the multiplication.
In essence:
for n=1:numel(S)
R*S{n}*R'
end
or using cellfun where #(x) is an anonymous function.
cellfun(#(x) R*x*R', S, 'UniformOutput',false)
I have a 480-by-640 matrix A. For each pixel, I want to check its neighbors. The neighbors of the pixel are determined by a value N. For example, this is a part of matrix A where all the zeros are the neighbours of pixel X when N=3:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 X 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
As shown, because N=3, all these zeros are pixel X's neighbors. The problem is if X is located before the index N=3. Here the neighbors will be pixels with one values:
X 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Could anyone advise on how to handle this?
The simplest way to proceed is just to pad your array with with values that do not return true for whatever you are checking (say, if you're looking for nonzeros, pad with zeros, or if you're looking for finite values, pad with NaN.) The padarray function can do this for you, but requires the Image Processing Toolbox*. Otherwise, you can pad arrays yourself. For example, an unoptimized way to proceed might be
A = rand(m,n);
Apadded = [zeros(N,2*N+n); [zeros(m,N), A, zeros(m,N)]; zeros(N,2*N+n)];
for i = N+1:N+m+1
for j = N+1:N+n+1
% Process neighborhood of A(i,j)
end
end
*Also note that these sorts of "sliding neighborhood" operations, being common in image processing, are implemented for you in the Image Processing Toolbox.