Matrix with dynamic dimension in matlab [closed] - matlab

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 3 years ago.
Improve this question
In a do-while loop until its end, in each iteration a new column should be added to a predefined matrix. I couldn't find how to define such a matrix in matlab? is there a standard method for defining such matrix with dynamic dimension?
Note: The do-while loop is simulated by a for loop
A=zeros(100,?);
for i=1:inf
A(:,i)=some computation ;
*condition*
end

Although the code here below works, you may consider #Sardar_Usama suggestion, working with cells. But I'm not too good with those...
A = zeros(100, 20); % initial number of columns is 20, but can grow larger
col = 1;
while(condition)
A(:, col) = result of some computation;
col = col + 1;
end;

matlab dynamically handles matrix sizes. So you can use add new columns or rows easily. However, it can degrade the performance of the algorithm. some examples:
A= ones(10,4);
B = zeros(10,2);
C = rand(3,4);
A = [A B]; % adds two new columns consisting of B to the end of A
A = [C; A];% adds three new rows consisting of C to the beginning of A

Related

Loop vectorization [closed]

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 2 years ago.
Improve this question
I'm applying the filtering function in the frequency domain. Because I am working with large amounts of data, I want to vectorize the for loop shown below. Any help would be appreciated.
N = 2^nextpow2(length(signal));
Y = fft(signal,N);
df=1/(N*dt);
freq=0:df:N/2*df-df;
ff=freq./fccutlow;
H=zeros(1,length(N));
for i=2:length(ff)
H(i)= sqrt((ff(i).^(2*nOrder)))./sqrt(((1+ff(i).^(2*nOrder))));
Y(i)= Y(i).*H(i);
Y(length(Y)-i+2)=Y(length(Y)-i+2).*H(i);
end
Y1= (real(ifft(Y,N)))';
Y1=Y1(1:length(signal));
filt_signal(1,:)=Y1;
For vector arithmetic on whole matrix you do not need any indexing, only use array operators, .*, ./, .^. But when only a part of array is used as operand, you need to use one of indexing methods. Keep in mind that all input and output ranges of arrays should be of compatible sizes.
Check parentheses again, but it is something like this:
H = sqrt((ff.^(2*nOrder)))./sqrt(((1+ff.^(2*nOrder))));
Y= Y.*H;
Y(end-((1:length(ff))+2))=Y(end-((1:length(ff))+2)).*H;
An example: Note that it does not matter if the matrices themselves do not have same sizes, but the operands must have compatible sizes:
A = 1:4; % 1x4
B = magic(3); % 3x3
C = zeros(3, 1); % 3x1
C([1 3]) = A(3:end).*B(1:2, 2)';
Although in this example A, B and C have different sizes, but A(3:end) and B(1:2, 2)' are both 1 by 2 and the code runs with no problem. Try to run it and evaluate different parts of code to see how indexing works.

Matlab: How do you implement this sum? [closed]

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 6 years ago.
Improve this question
I need to implement the following:
x_i = e ^ (-1 - sum (y_j * A_ji))
where i = 1..10, j = 1..5 and A is a 5x10 matrix (randomly generated).
I tried using symsum but it gave me an index error. Could someone please help me figure out how to implement this?
With
A = rand(5,10); %# random 5x10 array
y = rand(1,5); %# random 1x5 array
Your sum becomes
x = exp( -1 - y*A);
thanks to linear algebra.

Matlab Matrix Dataset Comparison and Certain Output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
if I have matrices A & B: A=[400(rows)x60(columns)] B=[150x60] (Note:That matrix B has fixed columns=60, but the rows are variable could change from 1-400)
I will enter these two matrices into matlab, matrix A is fixed and uploaded to matlab as an excel file, while matrix B is measured directly from the code.(Note:Both matrices contain numerical values)
Now what I want help with is the following: I want to compare the data I'm getting from matrix B with matrix A CONTINUOUSLY, if the values in B are CLOSE (NOT NECESSARILY EQUAL) to the values of A then the matlab outputs lets say True. If the values in B are NOT CLOSE or completely distant from the values of A, then output False. (Note: I don't know but maybe we should use a certain threshold to determine the range of closeness of values between A and B, if we should have a certain threshold, lets say the threshold should be 70%)
Please if anyone has an answer or could help, I need the program. I'm using Matlab 2014a. Thanks in advance.
Easy!.
Suppose you have your matrix in the file A.xlsx,sheet Sheet1 and the matrix B in matlab already.
Also, let's say that, under this context, that CLOSE is some operator between A and B, up to the m-th row, for example the norm, with m taking any value from 1 to 400.
Norm(A-Bm)=|A-Bm|= SUM_i=1^m SUM_j=1^60 (a(i,j)-bm(i,j))^2
Let's define a threshold, for example 0.1*Norm(An)*Norm(Bn).
Hence, the code for that is the following:
function result=matrixcomparison(Bm)
% Read A Matrix
A=xlsread('A');
[ma,na]=size(A); %ma is always 400, na is always 60
[mb,nb]=size(Bm); %mb is variant, na is always 60
% Make a Projection Matrix
Am=A(1:mb,1:nb);
%Compute the norm
normABm=norm(Am-Bm);
%Compare
if (normABm>0.1*norm(Am)*norm(Bm))
disp('Matrices are different.');
result= 0;
else
disp('Matrices are equal.');
result= 1;
end
You save the above code under the file matrixcomparison.m
Of course, this dont have any 'Continuous' comparison. which you should make on the following sense:
for m=1:400
% Read B
<<Calculate B for m>>
% Calculate Norm
result(m)=matrixcomparison(Bm);
end
% Plot the results
plot(results);
You paste the second above code onto another Script and just run it.
Cheers,...

Looping through columns and rows without using Indexing in matlab

I'm fairly new to matlab and I'm currently working on MATLAB to create a loop that will go through each column and each row and then increment A and B as it goes. I know that there's indexing which you can do but I'd like to learn how to do it step by step. I've come up with the pseudo code for it but I'm struggling with the actual syntax in MATLAB to be able to do it.
Pseudocode:
For columns i 1-300;
Increment A
For rows j 1-4
Increment B
End
End
My actual code that I've been trying to get to work is:
%testmatrix = 4:300 Already defined earlier as a 4 row and 300 column matrix
for i = testmatrix (:,300)
for j = testmatrix (4,:)
B=B+1
end
A=A+1
end
I'm not 100% sure how I'm supposed to format the code so it'll read testmatrix(1,1) all the way through to testmatrix (4,300).
Any help would be greatly appreciated!
You could let it run through the first row to get the right column, then through that column. But you can't feed your running value from the matrix:
[rows cols] = size(testmatrix); %// rows=4, cols=300
for i = 1:cols
for j = 1:rows
temp = testmatrix (j,i); %// contains the element of your matrix at (j,i)
B=B+1;
end
A=A+1;
end
The semicolons ; suppress the output to the command line. Remove them if you want to output A and B at each step.
Here, temp will cycle through the elements (1,1) through (4,300) and you can do whatever you want with them. Note that this is generally an inefficient way to do most things. Matlab supports greatly efficient vectorized calculations, which you should use. But unless I know what exactly you're trying to achieve, I can't really help you with that. (If all you want is A and B's final values, it's as easy as A=A+cols;B=B+cols*rows;.)

Constructing a matrix in matlab from values generated by for loop [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Matlab - building an array while looping
Matrix of unknown length in MATLAB?
How do you put all of the "a" values together to form a vector?
for i=1:3
a=2+i
end
Also this is maybe a style question but when do you put a semicolon after the end in a for loop such as the one above, also is it proper to put a semicolon after the first line?
You need to index into a, like this:
for ii=1:3
a(ii) = 2+ii;
end
I prefer to use ii as a loop variable to avoid clashing with MATLAB's built-in i. You should also pre-allocate a if you know the size before the start of the loop, like so:
N = 100;
a = zeros(1,N);
for ii=1:N
a(ii) = 2 + ii;
end
Personally, I never put any punctuation after the for ii=1:3 part, except when writing a one-liner FOR loop, like so:
for ii=1:N, a(ii) = 2 + ii; end
Note that you can construct this more efficiently as such:
a=1:3;
a=a+2;
The first line assigns a to be the vector (1,2,3), the 2nd line adds 2 to every element.
"Efficiency" doesn't matter much in such a small vector, but in general you'll get much better mileage out of matlab if you get used to thinking more like this.