cell-array

Find index of all (non-unique) elements in a cell array as they appear in a second (sorted and unique) cell array

隐身守侯 提交于 2019-11-28 01:19:34
A = {'A'; 'E'; 'A'; 'F'}; B = {'A';'B';'C';'D';'E'; 'F'}; I am trying to get for each string in cell array A , the index that matches that string in cell array B . A will have repeated values, B will not. find(ismember(B, A) == 1) outputs 1 5 6 but I want to get 1 5 1 6 preferably in a one liner. I can't use strcmp instead of ismember either as the vectors are different sizes. The vectors will actually contain date strings, and I need the index not a logical index matrix, I'm interested in the number not to use it for indexing. How do I do it? You flip the arguments to ismember , and you use

How to combine vectors of different length in a cell array into matrix in MATLAB

依然范特西╮ 提交于 2019-11-27 14:53:42
How to efficiently combined cell array vectors with different length into a matrix, filling the vectors to max length with 0s or NaNs? It would be a nice option for cell2mat() . For example, if I have C = {1:3; 1:5; 1:4}; I'd like to get either M = [1 2 3 0 0 1 2 3 4 5 1 2 3 4 0]; or M = [1 2 3 NaN NaN 1 2 3 4 5 1 2 3 4 NaN]; abcd EDIT: For a cell of row vectors as in your case, this will pad vectors with zeros to form a matrix out=cell2mat(cellfun(@(x)cat(2,x,zeros(1,maxLength-length(x))),C,'UniformOutput',false)) out = 1 2 3 0 0 1 2 3 4 5 1 2 3 4 0 A similar question was asked earlier today,

How do I detect empty cells in a cell array?

可紊 提交于 2019-11-27 11:32:25
问题 How do I detect empty cells in a cell array? I know the command to remove the empty cell is a(1) = [] , but I can't seem to get MATLAB to automatically detect which cells are empty. Background: I preallocated a cell array using a=cell(1,53) . Then I used if exist(filename(i)) and textscan to check for a file, and read it in. As a result, when the filename(i) does not exist, an empty cell results and we move onto the next file. When I'm finished reading in all the files, I would like to delete

Replace empty cells with logical 0's before cell2mat in MATLAB

一个人想着一个人 提交于 2019-11-27 01:21:38
问题 I have an array of empty cells and ones that I want to convert to a logical array, where the empty cells are zeros. When I use cell2mat, the empty cells are ignored, and I end up with a matrix of solely 1's, with no reference to the previous index they held. Is there a way to perform this operation without using loops? Example code: for n=1:5 %generate sample cell array mycellarray{n}=1; end mycellarray{2}=[] %remove one value for testing Things I've tried: mylogicalarray=logical(cell2mat

How to combine vectors of different length in a cell array into matrix in MATLAB

一个人想着一个人 提交于 2019-11-26 16:57:15
问题 How to efficiently combined cell array vectors with different length into a matrix, filling the vectors to max length with 0s or NaNs? It would be a nice option for cell2mat() . For example, if I have C = {1:3; 1:5; 1:4}; I'd like to get either M = [1 2 3 0 0 1 2 3 4 5 1 2 3 4 0]; or M = [1 2 3 NaN NaN 1 2 3 4 5 1 2 3 4 NaN]; 回答1: EDIT: For a cell of row vectors as in your case, this will pad vectors with zeros to form a matrix out=cell2mat(cellfun(@(x)cat(2,x,zeros(1,maxLength-length(x))),C,

How can I accumulate cells of different lengths into a matrix in MATLAB?

别说谁变了你拦得住时间么 提交于 2019-11-26 13:46:01
So, I have a cell-array of 1xN vectors of different lengths. I want to append them into a matrix so I can display them with imagesc . Obviously the matrix must be the width of the largest vector. My current code for this is below: tcell = {[1,2,3], [1,2,3,4,5], [1,2,3,4,5,6], [1], []}; lens = cellfun('length', tcell); rmat = NaN(length(tcell), max(lens)); for i = 1:length(tcell) rmat(i, 1:lens(i)) = tcell{i}; end Does anyone know a vectorized solution for this type of problem? I'm not really worried about the speed of this loop because of MATLAB's JIT. I'm just trying to expand my knowledge

How to search for a string in cell array in MATLAB?

爷,独闯天下 提交于 2019-11-26 05:17:26
问题 Let\'s say I have the cell array strs = {\'HA\' \'KU\' \'LA\' \'MA\' \'TATA\'} What should I do if I want to find the index of \'KU\' ? 回答1: I guess the following code could do the trick: strs = {'HA' 'KU' 'LA' 'MA' 'TATA'} ind=find(ismember(strs,'KU')) This returns ans = 2 回答2: >> strs = {'HA' 'KU' 'LA' 'MA' 'TATA'}; >> tic; ind=find(ismember(strs,'KU')); toc Elapsed time is 0.001976 seconds. >> tic; find(strcmp('KU', strs)); toc Elapsed time is 0.000014 seconds. SO, clearly strcmp('KU',

Difference between accessing cell elements using curly braces and parentheses

耗尽温柔 提交于 2019-11-26 04:43:53
问题 What is the difference between accessing elements in a cell array using parentheses () and curly braces {} ? For example, I tried to use cell{4} = [] and cell(4) = [] . In the first case it sets the 4 th element to [] , but in the second case it wiped out the cell element, that is, reduced the cell element count by 1. 回答1: Think of cell array as a regular homogenic array, whose elements are all cell s. Parentheses ( () ) simply access the cell wrapper object, while accessing elements using

How can I accumulate cells of different lengths into a matrix in MATLAB?

蹲街弑〆低调 提交于 2019-11-26 03:44:05
问题 So, I have a cell-array of 1xN vectors of different lengths. I want to append them into a matrix so I can display them with imagesc . Obviously the matrix must be the width of the largest vector. My current code for this is below: tcell = {[1,2,3], [1,2,3,4,5], [1,2,3,4,5,6], [1], []}; lens = cellfun(\'length\', tcell); rmat = NaN(length(tcell), max(lens)); for i = 1:length(tcell) rmat(i, 1:lens(i)) = tcell{i}; end Does anyone know a vectorized solution for this type of problem? I\'m not