Declaring two matrices in one line of code? Matlab 2014 - matlab

,A simple question but one I have yet to get a concrete answer. If I have two matrices, say A and B and I want to make them both the same size, say a 1x2 matrix of zeros. Is there a way to declare them both in one line of code? I ask because in my situation I will have over 10 matrices of the same size but I want an easier way to declare them.
So at first I might think it would look like the following (which is not valid):
A,B = zeros(1,2)

The deal()-function does exactly what you are looking for. You can distribute either one input to several output-variables or also distribute several input values to several output values. You need the first case:
[A, B] = deal(zeros(1,2));

Related

Find the difference of all the values from the first value with Matlab

This may be very easy question but I am a little bit stuck on that. Is there any easy and fast way to do that?
I will explain with example what I want.
Let us suppose a vector
a= [1,10,20,30,40,50,60,70,80];
I want another vector lets say
b= [10-1, 20-1, 30-1, 40-1, 50-1, 60-1,70-1,80-1 ];
and then divide all the elements of b by 15 and save in another vector lets say c.
This is just an example in real I have vectors with more than 100 elements so I want to make it automatic.
Thank you in advance.
I just tried the following. It works:
b = a(2:end) - a(1);
c = b/15;

Extract data using Matlab

How could I correlate the first (also second and third, etc) line of each function together? I meant I want to pick up the value of the first line of function 1, first line of function 2 and so on... Specially number of these lines (between two #...#) are not always equal.
Let's say the values between (# -#) is one matrix. I see that the problem is I need to break them down into different matrix and then equalize all these matrix to the same size by adding NaN values and then reconstruct them again. That is what I think but I dont know how to ask Matlab do these tasks.
Would you please give me a help?
Thank you very much !!

Passing values to a sparse matrix in MATLAB

Might sound too simple to you but I need some help in regrad to do all folowings in one shot instead of defining redundant variables i.e. tmp_x, tmp_y:
X= sparse(numel(find(G==0)),2);
[tmp_x, temp_y] = ind2sub(size(G), find(G == 0));
X(:)=[tmp_x, tmp_y];
(More info: G is a sparse matrix)
I tried:
X(:)=ind2sub(size(G), find(G == 0));
but that threw an error.
How can I achieve this without defining tmp_x, tmp_y?
A couple of comments with your code:
numel(find(G == 0)) is probably one of the worst ways to determine how many entries that are zero in your matrix. I would personally do numel(G) - nnz(G). numel(G) determines how many elements are in G and nnz(G) determines how many non-zero values are in G. Subtracting these both would give you the total number of elements that are zero.
What you are doing is first declaring X to be sparse... then when you're doing the final assignment in the last line to X, it reconverts the matrix to double. As such, the first statement is totally redundant.
If I understand what you are doing, you want to find the row and column locations of what is zero in G and place these into a N x 2 matrix. Currently with what MATLAB has available, this cannot be done without intermediate variables. The functions that you'd typically use (find, ind2sub, etc.) require intermediate variables if you want to capture the row and column locations. Using one output variable will give you the column locations only.
You don't have a choice but to use intermediate variables. However, if you want to make this more efficient, you don't even need to use ind2sub. Just use find directly:
[I,J] = find(~G);
X = [I,J];

Matlab: Multiple assignment through logical indexing

I am wondering if there is some way, how to multiple assign values to different variables according logical vector.
For example:
I have variables a, b, c and logical vector l=[1 0 1] and vector with values v but just for a and c. Vector v is changing its dimension, but everytime, it has the same size as the number of true in l.
I would like to assign just new values for a and c but b must stay unchanged.
Any ideas? Maybe there is very trivial way but I didn't figure it out.
Thanks a lot.
I think your problem is, that you stored structured data in an unstructured way. You assume a b c to have a natural order, which is pretty obvious but not represented in your code.
Replacing a b c with a vector x makes it a really easy task.
x(l)=v(l);
Assuming you want to keep your variable names, the simplest possibility I know would be to write a function:
function varargout=update(l,v,varargin)
varargout=varargin;
l=logical(l);
varargout{l}=v(l);
end
Usage would be:
[a,b,c]=update(l,v,a,b,c)

Fill cell array with a certain value in matlab

First question: say I have a 3x3 cell array, lets call it A. So, if I want to fill A{1:2, 1:2} with the same cell array, how do I do it. MatLab requires both sides of the '=' to have the same number of elements. How do I assign the same value (a 2x1 cell) to A{1:2, 1:2}, in a single instruction?
Second question: I want to create a probability generator (not sure if it's the correct term) that will pick between a certain amount of option, based on a prior probability. For example, say that I want to randomly pick between A, B, and C, based on the following probabilities:
P(A) = .4
P(B) = .5
P(C) = .1
How do I accomplish this?
For your first question, repmat should work well.
For an example, see http://www.mathworks.com/matlabcentral/answers/8977
For your second question, combine <, cumsum, and find. If you want a more detailed explanation, open a second question covering just the probability generation.