I am trying to incremen matrix power from 1:1:n in for loop in matlab but getting error "input must be scalar and square matrix" - matlab

I am tring to increment power of A matrix in for loop but getting error
"input must be a scalar or square matrix"
function [S] = myst()
st_1 =[0.1490 0 0.1723 0.1786 0.2015 0.1387 0.1600]';
for A = [0.5 0 0 0 0 0.5 0.2;0 0 0 0 0 0 0 ; 0 0 0 0.4 0.4 0 0;0 0 1 0.2 0.2 0 0;0 0 0 0.4 0.2 0 0.4;0 0 0 0 0 0.5 0;0.5 0 0 0 0.2 0 0.4]
for n = 1:1:77
A_n = A^n; % this A matrix is changing its index after every loop,
%i don't know why index of A_n is changing?
S = (A_n)*st_1;
if S(1,1) == st_1(1,1)
disp(n);
break
end
end
end
disp(A);
end

When writing a for loop like for A=[1,2,3;4,5,6;7,8,9],disp(A);end you are iterating the individual column of A. Just remove the outer for loop when you intend to process the full matrix:
function [S] = myst()
st_1 =[0.1490 0 0.1723 0.1786 0.2015 0.1387 0.1600]';
A = [0.5 0 0 0 0 0.5 0.2;0 0 0 0 0 0 0 ; 0 0 0 0.4 0.4 0 0;0 0 1 0.2 0.2 0 0;0 0 0 0.4 0.2 0 0.4;0 0 0 0 0 0.5 0;0.5 0 0 0 0.2 0 0.4]
for n = 1:1:77
A_n = A^n; % this A matrix is changing its index after every loop,
%i don't know why index of A_n is changing?
S = (A_n)*st_1;
if S(1,1) == st_1(1,1)
disp(n);
break
end
end
disp(A);
end

Related

How to delete all columns in matrix containing zeros in some rows in MATLAB

I have a matrix whose size is 4x16 and I need to remove all columns which have the value of first row is zero. For example: here is the matrix example :
X = [1 0 0 0; 0 1 0 0; 0 0.4 0 0; 0 0 0 0.8; 0.5 0 0 0];
The 2D matrix (X) looks like this:
1 0 0 0
0 1 0 0
0 0.4 0 0
0 0 0 0.8
0.5 0 0 0
So what I need is just X = [1 0 0 0;0.5 0 0 0 ] because the first row of these columns are different of Zero.
Just exclude that row numbers:
X = X(X(:,1) ~= 0, :);
X(:,1) ~= 0 is a boolean vector for the first column which is true if the value of the element would not be 0.

Store and plot two structures in MATLAB

I am working with structures that two of which are shown below; A and B. I need to store A and B in a way (possibly not using tables) that allows MATLAB to plot them similar to the images.
In my viewpoint, generating a sparse matrix and store the circles in it may be a solution. Here is a function which could generate a circle array in a sparse matrix with given structure & color. What you need is design a table which denotes the color of each item (0 means there a nothing in that position).
Function CircleArrays.m
function [Imgout] = CircleArrays(DataIn,r,s)
%%Draw a circle array with given colors
% Parameters:
% DataIn ---- structures & color
% r ---- the radius of each circle
% s ---- the sparsity of the data
% Demo:
% S=rand(10);
% r=20;
% sparsity=1;
% Img=CircleArrays(S,r,sparsity);
% figure()
% colormap(hot)
% imagesc(1-Img)
% axis equal
% axis off
%Define a pre-allocate sparse matrix
[m,n]=size(DataIn);
Imgout=spalloc(m*(2*r+1),n*(2*r+1),floor(n*(2*r+1)*m*(2*r+1)*s));
%Create the Mask
I = 1:(2*r+1); % Define the x and y coordinates of the basic mask
x = (I - r)-1;
y = (r - I)+1;
[X,Y] = meshgrid(x,y); % Create the mask
M = (X.^2 + Y.^2 <= r^2);
[xx,yy]=ind2sub(size(M),find(M == true));
%%Add circles into the matrix
for ii=1:m
for jj=1:n
if (DataIn(ii,jj) == 0)
continue;
else
MidX=(ii-1)*(2*r+1)+r+1;
MidY=(jj-1)*(2*r+1)+r+1;
Imgout(sub2ind(size(Imgout),MidX+xx-r-1,MidY+yy-r-1))=DataIn(ii,jj);
end
end
end
end
DEMO:
clc; clear;
%%Structures & configs
% define structures & color
S=[0 0 0 0 0 0 0 0 0 0 0 0 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 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 .6 0.6 .6 .6 .6 .6 .6 .6 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 0 0 0 0 0 0 1 0 0 0;
0 0 0 1 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 0 0 0 0 0 0 0 0 0 0 0 0;];
%set the radius of each circle and the sparsity of the data
r=20;
sparsity=0.25;
%generate the Structures
Img=CircleArrays(S,r,sparsity);
%%Draw the picture
figure()
colormap(hot)
imagesc(1-Img)
axis equal
axis off
output by DEMO code:
output by DEMO code in the function:
Notes: I wrote this function based on one of my old answers, so the circle mask maybe not 100% perfect, you can change it to whatever you like.

replace non-zero values with random numbers

I have a zero-one matrix in MATLAB as follows:
[0 0 0 1 1 1
0 1 1 0 0 0
1 0 0 0 0 1
1 1 1 0 0 0
0 0 0 1 0 1]
I want to define another matrix including rand values instead of indexes of above matrix by 1. For instance the desired new rand matrix should be:
[0 0 0 0.2 0.2 0.1
0 0.6 0.7 0 0 0
0.4 0 0 0 0 0.6
0.7 0.8 0.5 0 0 0
0 0 0 0.3 0 0.4]
I used a two nested loop for to find non-zero values from first matrix and replace the rand values instead of them in a new matrix.
Is there any function of matlab to do it automatically, without using two nested loop for?
You can do it as follows:
A = ...
[0 0 0 1 1 1;
0 1 1 0 0 0;
1 0 0 0 0 1;
1 1 1 0 0 0;
0 0 0 1 0 1];
B = rand(size(A));
A(logical(A)) = B(logical(A));
A =
0 0 0 0.1320 0.2348 0.1690
0 0.3377 0.3897 0 0 0
0.9027 0 0 0 0 0.7317
0.9448 0.3692 0.4039 0 0 0
0 0 0 0.0598 0 0.4509
(I just took the basic rand-function, adjust it, as you need it)
You can slightly improve thewaywewalk's answer by generating only as many random numbers as you need. As a bonus, this approach allows to do everything in one line:
A(logical(A)) = rand(1,nnz(A));
If you're trying to replace the ones in matrix A with random numbers then you don't need any looping at all.
Here's one method.
a = double(rand(5,5)>.5); % Your binary matrix should be type double.
n = sum(a(:)); % Count the 1's.
a(a>0) = rand(1,n); % Replace the ones with rands.
If 'l' is a matrix containing zeros and non-zeros. Consider the scenarios below answering this question :
Replace all the zeros in matrix with random numbers :
l(l==0) = randn(1,size(l(l==0),1));
Replace all the positive values with random numbers :
l(l>0) = randn(1,size(l(l>0),1));
Replace all the negative values with random numbers :
l(l<0) = randn(1,size(l(l<0),1));
Replace all the 'NaN' with random numbers.
l(isnan(l)) = randn(1,size(l(isnan(l)),1));

Drawing a line of ones on a matrix

I have a matrix m = zeros(1000, 1000). Within this matrix I want to draw an estimate of the line which passes through 2 points from my matrix. Let's say x = [122 455]; and y = [500 500];.
How can I do this in Matlab? Are there any predefined functions to do this? I am using Matlab 2012b.
I'll denote the two endpoints as p1 and p2 because I'm planning to use x and y for something else. I'm also assuming that the first coordinate of p1 and p2 is x and the second is y. So here's a rather simple way to do it:
Obtain the equation of the line y = ax + b. In MATLAB, this can be done by:
x = p1(1):p2(1)
dx = p2(1) - p1(1);
dy = p2(2) - p1(2);
y = round((x - p1(1)) * dy / dx + p1(2));
Convert the values of x and y to indices of elements in the matrix, and set those elements to 1.
idx = sub2ind(size(m), y, x);
m(idx) = 1;
Example
Here's an example for a small 10-by-10 matrix:
%// This is our initial conditon
m = zeros(10);
p1 = [1, 4];
p2 = [5, 7];
%// Ensure the new x-dimension has the largest displacement
[max_delta, ix] = max(abs(p2 - p1));
iy = length(p1) - ix + 1;
%// Draw a line from p1 to p2 on matrix m
x = p1(ix):p2(ix);
y = round((x - p1(ix)) * (p2(iy) - p1(iy)) / (p2(ix) - p1(ix)) + p1(iy));
m(sub2ind(size(m), y, x)) = 1;
m = shiftdim(m, ix > iy); %// Transpose result if necessary
The result is:
m =
0 0 0 0 0 0 0 0 0 0
0 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 1 1 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 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Update: I have patched this algorithm to work when dy > dx by treating the dimension with the largest displacement as if it were the x-dimension, and then transposing the result if necessary.
Neither of the provided answers work for displacements in y greater than in x (dy > dx).
As pointed out, Bresenham's line algorithm is exactly meant for that.
The matlab file provided here works similarly than the examples provided in the other answers but covers all the use-cases.
To relate to the previously provided example, the script can be used like this:
% initial conditions
m = zeros(10);
p1 = [1, 4];
p2 = [5, 10];% note dy > dx
% use file provided on file exchange
[x y] = bresenham(p1(1),p1(2),p2(1),p2(2));
% replace entries in matrix m
m(sub2ind(size(m), y, x)) = 1;
result looks like this:
m =
0 0 0 0 0 0 0 0 0 0
0 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 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 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
For me (matlab R2013b) following line did not work, when p1(1)>p2(2) (":" can not count backwards):
x = p1(1):p2(1);
E.G.:
1:10
1 2 3 4 5 6 7 8 9 10
10:1
Empty matrix: 1-by-0
But it worked when I used linspac instead:
x = linspace(p1(1), p2(1), abs(p2(1)-p1(1))+1);

bicubic interpolation of 3D surface

I am writing an algorithm in matlab to for bicubic interpolation of a surface Psi(x,y). I have a bug in the code and cannot seem to track it down. I am trying a test case with Psi=X^2-0.25 so that its easier to track down the bug. It seems as if my interpolation has an offset. My comments are included in my code. Any help would be appreciated.
Plot of Psi=X^2 in blue and interpolation in red
Countour lines of Psi are plotted and the red dot is the point I am computing the interpolation about. The thick red line is the interpolation which is offset quite a bit from the red dot.
function main()
epsilon=0.000001;
xMin=-1+epsilon;
xMax= 1+epsilon;
yMin=-1+epsilon;
yMax= 1+epsilon;
dx=0.1; Nx=ceil((xMax-xMin)/dx)+1;
dy=0.1; Ny=ceil((yMax-yMin)/dy)+1;
x=xMin:dx:xMax; x=x(1:Nx);
y=yMin:dy:yMax; y=y(1:Ny);
[XPolInX,XPolInY]=GetGhostMatricies(Nx,Ny); %Linear extrapolation matrix
[D0x,D0y]=GetDiffMatricies(Nx,Ny,dx,dy); %derivative matricies: D0x is central differencing in x
[X,Y]=meshgrid(x,y);
Psi=X.^2-0.25; %Note that my algorithm is being written for a Psi that may not have a analytic representation. This Psi is only a test case.
psi=zeros(Nx+2,Ny+2); %linearly extrapolate psi (for solving differential equation not shown here)
psi(2:(Nx+1),2:(Ny+1))=Psi';
psi=(XPolInY*(XPolInX*psi)')';
%compute derivatives of psi
psi_x =D0x*psi; psi_x =(XPolInY*(XPolInX*psi_x)')';
psi_y =(D0y*psi')'; psi_y =(XPolInY*(XPolInX*psi_y)')';
psi_xy=D0x*psi_y; psi_xy=(XPolInY*(XPolInX*psi_xy)')';
% i have verified that my derivatives are computed correctly
biCubInv=GetBiCubicInverse(dx,dy);
i=5; %lets compute the bicubic interpolation at this x(i), y(j)
j=1;
psiVoxel=[psi( i,j),psi( i+1,j),psi( i,j+1),psi( i+1,j+1),...
psi_x( i,j),psi_x( i+1,j),psi_x( i,j+1),psi_x( i+1,j+1),...
psi_y( i,j),psi_y( i+1,j),psi_y( i,j+1),psi_y( i+1,j+1),...
psi_xy(i,j),psi_xy(i+1,j),psi_xy(i,j+1),psi_xy(i+1,j+1)]';
a=biCubInv*psiVoxel; %a=[a00 a01 ... a33]; polynomial coefficients; 1st index is power of (x-xi), 2nd index is power of (y-yj)
xi=x(5); yj=y(1);
clear x y
x=(xi-.2):.01:(xi+.2); %this is a local region about the point we are interpolating
y=(yj-.2):.01:(yj+.2);
[dX,dY]=meshgrid(x,y);
Psi=dX.^2-0.25;
figure(2) %just plotting the 0 level contour of Psi here
plot(xi,yj,'.r','MarkerSize',20)
hold on
contour(x,y,Psi,[0 0],'r','LineWidth',2)
set(gca,'FontSize',14)
axis([x(1) x(end) y(1) y(end)])
grid on
set(gca,'xtick',(xi-.2):.1:(xi+.2));
set(gca,'ytick',(yj-.2):.1:(yj+.2));
xlabel('x')
ylabel('y')
[dX dY]=meshgrid(x-xi,y-yj);
%P is my interpolating polynomial
P = a(1) + a(5) *dY + a(9) *dY.^2 + a(13) *dY.^3 ...
+ a(2)*dX + a(6)*dX .*dY + a(10)*dX .*dY.^2 + a(14)*dX .*dY.^3 ...
+ a(3)*dX.^2 + a(7)*dX.^2.*dY + a(11)*dX.^2.*dY.^2 + a(15)*dX.^2.*dY.^3 ...
+ a(4)*dX.^3 + a(8)*dX.^3.*dY + a(12)*dX.^3.*dY.^2 + a(16)*dX.^3.*dY.^3 ;
[c h]=contour(x,y,P)
clabel(c,h)
figure(3)
plot(x,x.^2-.25) %this is the exact function
hold on
plot(x,P(1,:),'-r*')
%See there is some offset here
end
%-------------------------------------------------------------------------
function [XPolInX,XPolInY]=GetGhostMatricies(Nx,Ny)
XPolInX=diag(ones(1,Nx+2),0);
XPolInY=diag(ones(1,Ny+2),0);
XPolInX(1,1) =0; XPolInX(1,2) =2; XPolInX(1,3) =-1;
XPolInY(1,1) =0; XPolInY(1,2) =2; XPolInY(1,3) =-1;
XPolInX(Nx+2,Nx+2)=0; XPolInX(Nx+2,Nx+1)=2; XPolInX(Nx+2,Nx)=-1;
XPolInY(Ny+2,Ny+2)=0; XPolInY(Ny+2,Ny+1)=2; XPolInY(Ny+2,Ny)=-1;
fprintf('Done GetGhostMatricies\n')
end
%-------------------------------------------------------------------------
function [D0x,D0y]=GetDiffMatricies(Nx,Ny,dx,dy)
D0x=diag(ones(1,Nx-1),1)-diag(ones(1,Nx-1),-1);
D0y=diag(ones(1,Ny-1),1)-diag(ones(1,Ny-1),-1);
D0x(1,1)=-3; D0x(1,2)=4; D0x(1,3)=-1;
D0y(1,1)=-3; D0y(1,2)=4; D0y(1,3)=-1;
D0x(Nx,Nx)=3; D0x(Nx,Nx-1)=-4; D0x(Nx,Nx-2)=1;
D0y(Ny,Ny)=3; D0y(Ny,Ny-1)=-4; D0y(Ny,Ny-2)=1;
%pad with ghost cells which are simply zeros
tmp=D0x; D0x=zeros(Nx+2,Nx+2); D0x(2:(Nx+1),2:(Nx+1))=tmp; tmp=0;
tmp=D0y; D0y=zeros(Ny+2,Ny+2); D0y(2:(Ny+1),2:(Ny+1))=tmp; tmp=0;
%scale appropriatley by dx & dy
D0x=D0x/(2*dx);
D0y=D0y/(2*dy);
end
%-------------------------------------------------------------------------
function biCubInv=GetBiCubicInverse(dx,dy)
%p(x,y)=a00+a01(x-xi)+a02(x-xi)^2+...+a33(x-xi)^3(y-yj)^3
%biCubic*a=[psi(i,j) psi(i+1,j) psi(i,j+1) psi(i+1,j+1) psi_x(i,j) ... psi_y(i,j) ... psi_xy(i,j) ... psi_xy(i+1,j+1)]
%here, psi_x is the x derivative of psi
%I verified that this matrix is correct by setting dx=dy=1 and comparing to the inverse here http://en.wikipedia.org/wiki/Bicubic_interpolation
biCubic=[
%00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
1 dx dx^2 dx^3 0 0 0 0 0 0 0 0 0 0 0 0;
1 0 0 0 dy 0 0 0 dy^2 0 0 0 dy^3 0 0 0;
1 dx dx^2 dx^3 dy dx*dy dx^2*dy dx^3*dy dy^2 dx*dy^2 dx^2*dy^2 dx^3*dy^2 dy^3 dx*dy^3 dx^2*dy^3 dx^3*dy^3;
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 1 2*dx 3*dx^2 0 0 0 0 0 0 0 0 0 0 0 0;
0 1 0 0 0 dy 0 0 0 dy^2 0 0 0 dy^3 0 0;
0 1 2*dx 3*dx^2 0 dy 2*dx*dy 3*dx^2*dy 0 dy^2 2*dx*dy^2 3*dx^2*dy^2 0 dy^3 2*dx*dy^3 3*dx^2*dy^3;
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 1 dx dx^2 dx^3 0 0 0 0 0 0 0 0;
0 0 0 0 1 0 0 0 2*dy 0 0 0 3*dy^2 0 0 0;
0 0 0 0 1 dx dx^2 dx^3 2*dy 2*dx*dy 2*dx^2*dy 2*dx^3*dy 3*dy^2 3*dx*dy^2 3*dx^2*dy^2 3*dx^3*dy^2;
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 1 2*dx 3*dx^2 0 0 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 2*dy 0 0 0 3*dy^2 0 0;
0 0 0 0 0 1 2*dx 3*dx^2 0 2*dy 4*dx*dy 6*dx^2*dy 0 3*dy^2 6*dx*dy^2 9*dx^2*dy^2];
biCubInv=inv(biCubic);
end
%-------------------------------------------------------------------------
I found my error. I pad my matricies with ghost cells, however I forget that now the i in Psi without ghost cells is i+1 in psi with ghost cells. Hence, I should be evaluating my interpolating polynomial P at xi=x(6); yj=y(2), not xi=x(5); yj=y(1).