Print/Store each row in matrix - MATLAB - matlab

I have a 150 X 4 matrix and I wish to loop through the matrix length and print each of the rows out.
This is my attempted code:
X = xlsread('filename.csv');
J = X(:, [2:5]) % extracting rows 2 to 5 into a matrix
for i= 0:length(J)
Y = J(i,:); %loop through each row and store it in Y
end;
But I keep getting the following error:
Subscript indices must either be real positive integers or logicals.
Is my approach incorrect? What am I missing out here? I simply want to loop through each row and store it in a variable.

In MATLAB the indices start at 1 and not at 0, so you should do:
for i= 1:length(J)
Y = J(i,:); %loop through each row and store it in Y
end;
In addition, regarding the following line that you wrote:
J = X(:, [2:5]) % extracting rows 2 to 5 into a matrix
Note that you actually stored in J columns 2,3,4,5, of X and not rows 2,3,4,5.

Related

Matlab matrix accessing columns

Can someone explain what is happening here?
I know that Y(:,1) is the first column of values in Y.
But what does Y(p,1) mean?
Matlab noob
Y = load('testFile.txt'); %Load file
p = randperm(length(Y)); %List of random numbers between 0 and Y size
Y(:,1) = Y(p,1);
Y(:,2) = Y(p,2);
Y(:,3) = Y(p,4);
Y = load('testFile.txt'); %Load file
% create a mapping to shuffle the rows of Y.
% This assumes more rows than columns in Y, otherwise the code doesn't make sense.
% This is because length(Y) returns the length of the largest dimension.
% A more robust implementation would use size(Y,1) rather than length(Y).
p = randperm(length(Y)); %List of random numbers between 0 and Y size
% Rearrange the rows of column 1 based on the shuffled order
Y(:,1) = Y(p,1);
% Rearrange the rows of column 2 based on the shuffled order
Y(:,2) = Y(p,2);
% Set the third column to the shuffled fourth column.
Y(:,3) = Y(p,4);

permutations of vectors in matrix in matlab

I have a static matrix from a file:
size(data)=[80 5]
What I want is to change position of each vector randomly
when I use perms like:
N = size(data, 1);
X = perms(1:N); % # Permutations of column indices
Y = meshgrid(1:N, 1:factorial(N)); % # Row indices
idx = (X - 1) * N + Y; % # Convert to linear indexing
C = data(idx)
But its giving me an error: Maximum variable size allowed by the program is
exceeded.
Is there any other function to give me what I need?
perms is not good for large numbers i.e any number >10
Refer the Documentation
It says
perms(v) is practical when length(v) is less than about 10.
See the size it takes from the following code:
MB(10,1) = 0;
for N = 1:10
X = perms(1:N);
dt=whos('X');
MB(N)=dt.bytes*9.53674e-7;
end
plot(1:10,MB,'r*-');
Note the sudden rise in the steepness of the curve beyond 9.
So i think im done
N = size(data, 1);
r=randperm(N);
for ii=1:80
matrix(r(ii),:) =data(ii,:) ;
end

Matlab: store array in matrix?

I have many array (n*1 dimension), how can I do something like
matrix = [];
for i = 1:5
for j =1:5
matrix (i,j) = zeros(n,1); % store a given array to a cell of a matrix
end
end
I find Array of Matrices in MATLAB
But this is store matrices into array, not the otherwise.
Ying Xiong's suggestion is what you want if the vectors are of different lengths. But assuming the number of elements is constant (which they seem to be) you may also use a 3-dimensional array, where each (i,j) element contains a vector in the third dimension, like this:
rows = 5; cols = 5; n = 10; %// Dimensions
matrix = zeros(rows, cols, n); %// Initialize matrix
vector = 1:n; %// Just an example
for ii = 1:rows %// Bad practice to use i as a variable name
for jj = 1:cols %// Bad practice to use j as a variable name
matrix(ii,jj,:) = vector; %// Assignment
end
end
Now each index (i,j) contains the vectors you want, for instance:
squeeze(matrix(1,1,:))
ans =
1
2
3
4
5
6
7
8
9
10
Having all values in a single matrix can be a good thing if you want to do similar operations on all elements, as vectorized approaches are usually very fast in MATLAB. You might want to check out permute, reshape and functions like bsxfun.
Note that you might be able to vectorize the loops, but without knowing the specifics, that's impossible to know.
You need to use cell array.
n = 10;
matrix = cell(5,5);
for i = 1:5
for j = 1:5
matrix{i,j} = zeros(n,1);
end
end

using matrix elements as indices into another matrix

I have an m by n matrix of 0s called weightmat.
I have an m by k matrix of unique random integers called placeIn, where k < n, and the largest element in placeIn is <= n.
I am trying to place the elements of placeIn into weightmat, using their values as row indices. If a certain row of placeIn has a 4 in it, I want 4 to be placed in the 4th column of the corresponding row of weightmat. Here's example code that does what I'm saying:
% create placeIn
placeIn = [];
for pIx = 1:5
placeIn = [placeIn; randperm(9,3)];
end
display(placeIn)
weightmat = zeros(5,10);
for pIx = 1:5
for qIx = 1:3
weightmat(pIx,placeIn(pIx,qIx)) = placeIn(pIx,qIx);
end
end
display(weightmat)
Is there a vectorized way to do this? I would like to accomplish this without the nested for loops.
The trick is sub2ind:
% First generate the row indices used for the indexing. We'll ignore the column.
[r c] = meshgrid(1:size(placeIn, 2), 1:size(placeIn,1));
weightmat = zeros(5,10);
% Now generate an index for each (c, placeIn) pair, and set the corresponding
% element of weightmat to that value of placeIn
weightmat(sub2ind(size(weightmat), c, placeIn)) = placeIn;

Matlab d-dimensional multipication table?

I'm new to Matlab and I'm trying to solve a problem that involves creating a d dimensional multiplication table where each edge goes from 1 to n. The problem statement says that inputting d = 0 should return the number 1 and d = 1 should return a column vector with the elements 1 to n.
Ideally, I would just create a matrix of 1 to n along d dimensions and then iterate through for each element setting it equal to the product of the indices, but I don't know how to create the d dimensional matrix.
Can anyone help me with this problem?
You can create the table with repeated use of bsxfun. At each iteration, the vector 1,2,...,n is shifted to a new dimension and multiplied (with singleton expansion) by the previous result.
%// Data
d = 3;
n = 10;
%// Computations
vector = (1:n).'; %// first dimension: column vector
result = 1; %// initialization
for n = 1:d
result = bsxfun(#times, result, vector); %// new dimension
vector = shiftdim(vector,-1); %// shift to the next dimension
end