I am currently working on machine learning, and as so I have an array in which the first column is the data and the second column is the label. Data was originally a cell array from Matlab (Not sure if that is important).
[My Array of arrays] https://i.stack.imgur.com/JFpWO.png
To make sure that everything is as it should be I would like to extract one of the arrays in index 0 and check its dimensions with the numpy.shape function. Currently, if I try that I just get the shape of the bigger array IE. (394,2)
Any ideas?
There are different ways.: Try..
arr[:,1,:]
or
arr[:,1]
or
[a[1] for a in arr]
Related
I'm trying to delete multiple consecutive and non-consecutive columns from a 80-column, 1-row cell array mycells. My question is: what's the correct indexing of a vector of columns in Matlab?
What I tried to do is: mycells(1,[4:6,8,9]) = [] in an attempt to remove columns 4 to 6, column 8 and 9. But I get the error: A null assignment can have only one non-colon index.
Use a colon for the first index. That way only the 2nd index is "non-colon". E.g.,
mycells(:,[4:6,8,9]) = []
MATLAB could have been smart enough to recognize that when there is only one row the 1 and : amount to the same thing and you will still get a rectangular array result, but it isn't.
Before getting the above VERY VERY HELPFUL AND MUCH SIMPLER answers, I ended up doing something more convoluted. As it worked in my case, I'll post it here for anyone in future:
So, I had a cell array vector, of which I wanted to drop specific cells. I created another cell array of the ones I wanted to remove:
remcols = mycells(1,[4:6,8,9])
Then I used the bellow function to overwrite onto mycells only those cells which are different between remcols and mycells (these were actually the cells I wanted to keep from mycells):
mycells = setdiff(mycells,remcols)
This is not neat at all but hopefully serves the purpose of someone somewhere in the world.
I wrote several cell arrays I read from txt-files in cell arrays within the MATLAB Workspace. I wrote a cell array 'Names', containing the names of the other cell arrays I assigned. The txt-files can have varying names, therfore I want to use this dynamic naming.
Now I would like to the cell arrays dynamically by using the name (string) from 'Names'. Is this possible? Which syntax do I have to use?
Logically Names{i} gives back the name of the cell array in position i. However using Names{i}.n does not work.
I could not find anything in the MATLAB documentation either..
a quite basic question. How do you replace a dimension of an array with a dataframe of the correct size (=matching the array in the other two dimensions)?
A simple working example: I make a small array of nrow=2, ncol=3, and 2 dimensions. I make a small dataframe which is also nrow=2 and ncol=3. If I try to replace one of the array's dimensions with the dataframe, it doesn't work properly and the array's dimensions are all messed up. What am I doing wrong?
arr<-array(1:12, dim=c(2,3,2))
df<-data.frame(13:14, 15:16, 17:18)
arr[,,2]<-df
dim(arr)
I have a 60x60x35 array and would like to calculate the Wilcoxon signed rank test to calculate if the median for each element value across the third array dimension (i.e. with 35 values) is different from zero. Thus, I would like my results in two 60x60 arrays - with values of 0 and 1 depending on the test statistic, and in a separate array with corresponding p values.
The problem I am facing is specifying the command in a way that desired output would have appropriate dimensions and would be calculated across the appropriate dimension of the array.
Thanks for your help and all the best!
So one way to solve your problem is using a nested for-loop. Lets say your data is stored in data:
data=rand(60,60,35);
size_data=size(data);
p=zeros(size_data(1),size_data(2));
p(:,:)=NaN;
h=zeros(size_data(1),size_data(2));
h(:,:)=NaN;
for k=1:size_data(1)
for l=1:size_data(2)
tmp_data=data(k,l,:);
tmp_data=reshape(tmp_data,1,numel(tmp_data));
[p(k,l), h(k,l)]=signrank(tmp_data);
end
end
What I am doing is I preallocate the memory of p,h as a 60x60 matrix. Then I set them to NaN, so if you can easily see if sth went wrong (0 would be an acceptable result). Now I loop over all elements and store the actual data array in a new variable. signrank needs the data to be an array so I reshape it to two dimensions.
I guess you could skip those loops by using bsxfun
I'm trying to create a Multi dimensional array so I can create a waterfall plot in octave, a matlab type of program. (Please note octave does not have the waterfall plot option so I have to make a work around.
I have multiple arrays that have frequency in the first column and amplitude in the second column.
example: When looped through the arrays, which are called sort_array they each need to be placed into a multidimensional array on a separate page.
4000, .5
3002, .1234
1093, .7
I was trying to have each of these arrays (sort_array) added to a single multidimensional array by using a for loop to have everything added to one array to make it easier to plot and export as a text file. I also thought using k as the page option for the multiple dimension array. But I keep getting dimensions mismatch. Any ideas how to fix this?
Please note I left out the sort_array code and just included the example array called sort_array above as an example of what the array would look like. There will be about 9000 of them.
md=[];
for k=1:9000
md_tmp=[sort_array(:,1),sort_array(:,2)]
ma(:,:,k)=[ma;ma_tmp];
end
So when I type in ma(:,:,1) I would get
4000, .5
3002, .1234
1093, .7
and if I type in ma(:,:,2) I would get the next one.
thanks
Your error is here: ma(:,:,k)=[ma;ma_tmp];
You are mixing up two concepts.
Either concatenate the new matrix onto the current one:
ma = [ma; ma_tmp];
OR
Assign the new matrix directly to the correct index (this is the cleaner, more efficient solution):
ma(:,:,k) = ma_tmp;
But you can't do both.
thanks I found a work around
sort_array=sortrows(arraytmp,-1); %sort by freq
k_tmp=repmat(k,length(sort_array(:,2)),1); %to create page for freq and amp in multidimensional array
ma_tmp=[sort_array(:,1),sort_array(:,2),k_tmp];
ma=[ma;ma_tmp];