How to store all minimum values of every single column of two dimensional matrix and store their corresponding minimum indices

Hello ,
I am really a new learner in matlab simulation!!!!
I want to get the the minimum value of every single column that is in a two dimentional matix of
number. I used min function and it returns the minimum of every single column as a
vector. I want to store every minimum value in a array and their corresponding minimum indices in a different array .
Can you tell me how to do that ???
Thank you,

 Accepted Answer

x = rand(5, 5);
[value, index] = min(x);
This is explained in the excellent documentation:
help min
doc min
[EDITED] For multiple indices, if a minimum value appears repeatedly:
value = min(x);
n = size(x, 2);
indexC = cell(1, n);
for iC = 1:n
indexC{iC} = find(x(:, iC) == value(iC));
end
Now you need a cell, because each column can have a different number of replies.

10 Comments

Thank you for your answer!!!!
I am sorry for not using meaningful tags for my question.
I have already used min command like u mentioned .However it stord only one smallest value among the matrix and one column and row index for that particular smallest value.
I want to store all min value corresponding to every column of that matrix Will it store all the minimum value and index in two different specific arrays??
Please read the documentation. It explains clearly, that min operates along the first non-singelton dimension, which are the columns in case of a matrix. Then simply run the code I have posted and you get back the two arrays value and index, which contain the smallest value of each column and it column index.
Do you want to get multiple indices, if the minimum value appears repeatedly? Then please explain this explicitly.
yeah exactly!!! I want to get multiple indices .I will read the documentation throughly.
Thank you for your answer!!!! It is working bt I am not getting the complete result. it shows 1*19 number however I need 75*19 result...Because I am doing this loop from k= 1 :NumWin-1 where my Number of window is 76.
Can I post my code here...???
Yes, post is welcome here. But formatting it seems to be not trivial. Insert blank lines before and after the code, select the code with the mouse and hit the "{} Code" button, whatever the curly braces could mean here.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% time series decomposition by state space modelling %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Andreas Galka %%% ISM Tokyo / Dept. of Neurology Kiel %%% 2003-... %%% %%% new implementation "compartment2008" %%% this file "c8_script" : 8.VI.2011 : EP SEIZ MOD
clear all; close all;
warning off;
if(isunix); separator = '/'; else separator = '\'; end this_dir = pwd; dts_dir = [this_dir separator 'dts']; addpath(dts_dir);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% load data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% length_data=306155; % offset=0;len_ext=4000; % while offset<=length_lim % offset = offset+2000; % offset and len_ext refer to data resolution BEFORE subsampling % while len_ext<=length_data % len_ext = len_ext+2000; % 12000; % -1 : use length as given by data % end % end len_ext=4000;offset=0; if offset<length_data offset=offset+len_ext; end subsampfact = 1; chosen_channels = 1:19;%[1:20]; standardise_mean = 1; standardise_global_stadev = 1; standardise_channelwise_stadev = 0;
%%% % eegplot(data.data(channels,:),'srate',1000,'winlength',20);
[data,extdata,information,sources,mixmatr,mdata,stimfreqs] = dataload_neuroped(offset,len_ext,subsampfact,chosen_channels,standardise_mean,standardise_global_stadev,standardise_channelwise_stadev);
% content of 'information': % obsdim % obsdimlog2pi % chosen_channels % extdim % numpar % len % loglen % numtimser % offset % samplingrate % samplingtime % freqfac = 2*pi/samplingrate % coredatasetname % datasetname % channelnames % missvalpos % mtllcorr
% mdata : averaged stimulus response
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% channelwise linear analysis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
output_f = 0; % output figures output_t = 1; % output text
spectral_analysis(data,information,[output_f,output_t]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% main analysis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
do_pretransformation = 0; % 0 - no pretransformation ; 1 - just PCA ; 2 - PCA and rescaling ; 3 - FastICA ; 4 - MILCA
%%% MAR modelling
mult_auto_regr = 0; % multivariate autoregressive modelling modelorder_ext = 101; % select MAR model order; 0 or -ve means: automatic selection by AIC numofstates_toselect = 0; % for automatic selection of components from MAR model; if zero then manual selection: inds_k_extern = []; %[ 10 11 13 23 38 58 76 89 105 130 ]; % [15 16 17 33 45 60 75 ]; % manually selected components from MAR model; this must be [] if automatic selection
%%% state space modelling
kalman_filtering = 0; % Kalman Filtering; possibly including optimisation if flags are also set load_solution_from_file = 1; % use previously saved solution create_tailored_solution = 0; % for epilepsy_diaries project added_channels = []; % these channels are to be added; must already be included in "chosen_channels"; % added channels will always be added after the present channels, never inserted remove_channels = []; % must still be included in "chosen_channels" add_garch = 0; % load a non-GARCH solution and add GARCH machines armodnum = [0 4 0 0 0 2]; % [0 4 0 0 0 1]; % number of AR(p) processes for p = 1,2,3,... gmo = [0 0]; %[1 1]; % GARCH model orders: AR part, MA part ; note that some combinations are stupid, although possible, like [1 0]. [0 0] is non-GARCH, [1 1] is standard-GARCH garch_compartments = zeros(1,sum(armodnum)); % 1 if compartment will have a GARCH machine; zeros(1,sum(armodnum)) if non-GARCH extda_compartments = zeros(1,sum(armodnum)); % [0 0 0 0 0 0 1]; % 1 if compartment receives input from external signal % but this indicator field is not important, it can all be stored in the individual compartments compartments_to_remove = []; % kick out unwanted components omitting_each_compartment = 0; % here we can check how important (in terms of AIC) the individual components are create_stim_compartment = 0; % special option for finding evoked potential / stimulus component (not yet fully developed) compartments_to_merge = []; % form higher-order component from a set of present components extdatamodelorder = 0; % how many lags of external data to include into model
extract_comp_from_innov = 0; % extract more structure from the innovations modelorder_ext_i = 111; numofstates_toselect_i = 0; inds_k_extern_i = [2];
turn_comp_into_higher_order = 0; % can also turn into lower order comp_to_turn = 3; new_comp_order = 8;
optimise = 0; % optimisation by conventional routines with masking temp_file_to_load = []; % in case of retrieving intermediate solution, if optimisation was interrupted: number of hic_*.mat file to load, or [] rewrite_solution = 0; % rewrite solution without optimising mask_eigenvalues = [ ones(1,armodnum(2)) ; ... % do not forget to keep this up-to-date manually ones(1,armodnum(2)) ]; % mask_eigenvalues = [ ones(1,armodnum(2)+6*armodnum(12)) ; ... % do not forget to keep this up-to-date manually % ones(1,armodnum(2)+6*armodnum(12)) ]; % mask_eigenvalues = [ ones(1,armodnum(2)+5*armodnum(10)) ; ... % do not forget to keep this up-to-date manually % ones(1,armodnum(2)+5*armodnum(10)) ]; % mask_eigenvalues = [ ones(1,armodnum(2)+3*armodnum(6)) ; ... % do not forget to keep this up-to-date manually % ones(1,armodnum(2)+3*armodnum(6)) ];
state_init_ext = [];
manymarmodels = 0;
% non-standard choices for the global optimisation mask need to be explicitly defined in dts_mainfunction
%%% other peoples' ICA
do_milca = 0; % mutual information least dependent component analysis do_milca_delay = 0; % mutual information least dependent component analysis with delay do_factor_analysis = 0; % traditional (instantaneous) factor analysis (algorithm of Joereskog 1967) do_icang = 0; % instantaneous nongaussian ICA (maximum likelihood) do_fastica = 1; % Hyvaerinen's ICA (instantaneous) do_runica = 0; % InfoMax, plain or extended do_jade = 0; % Cardoso's JADE do_thinica = 0; % Cruces's ThinICA do_unibss = 0; % Hyvaerinen's UniBSS (not well developed) do_tdsep = 0; % Ziehe & Mueller GMD FIRST Berlin
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Computing Mixing Matrix %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% offset=len_ext+1; interval=offset:2000:length_data-offset ; %data=data(chosen_channels,interval); [NumChan NumSamples ]= size(data); NumWin=(length_data-2155)/len_ext ; mixmatr_ICA_st = zeros(NumChan,NumChan,NumWin); for k= 1:NumWin offset = (k-1)*len_ext; [data,extdata ,information ,sources ,mixmatr ,mdata ,stimfreqs] = dataload_neuroped(offset,len_ext,subsampfact,chosen_channels,standardise_mean,standardise_global_stadev,standardise_channelwise_stadev); [sources_ICA , mixmatr_ICA] = c8_ssm(data,extdata,information,state_init_ext,sources,mixmatr,output_f,output_t,do_pretransformation,mult_auto_regr,modelorder_ext,numofstates_toselect,inds_k_extern,kalman_filtering,load_solution_from_file,create_tailored_solution,add_garch,armodnum,gmo,garch_compartments,extda_compartments,compartments_to_remove,omitting_each_compartment,create_stim_compartment,compartments_to_merge,extdatamodelorder,extract_comp_from_innov,modelorder_ext_i,numofstates_toselect_i,inds_k_extern_i,turn_comp_into_higher_order,comp_to_turn,new_comp_order,optimise,temp_file_to_load,rewrite_solution,mask_eigenvalues,do_milca,do_milca_delay,do_factor_analysis,do_icang,do_fastica,do_runica,do_jade,do_thinica,do_unibss,do_tdsep,mdata,added_channels,remove_channels,stimfreqs,manymarmodels); mixmatr_ICA_st(:,:,k) = mixmatr_ICA; save mixmatr_ICA_st.mat mixmatr_ICA_st; end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Computing Euclidean distance between each window of Mixing Matirx %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for k= 1:NumWin-1 [NumChan NumSamples ]= size(data); X=mixmatr_ICA_st(:,:,k); Y=mixmatr_ICA_st(:,:,k+1); D=zeros(NumChan ,NumChan,NumWin); D(:,:,k)=sqrt((mixmatr_ICA_st(:,:,k) -mixmatr_ICA_st(:,:,k+1)).^2); B = reshape(D(:,:,k),[19 19]);
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Histogram Plotting of Distance Matrix%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % for k=1:NumWin-1 % dd=zeros(NumWin-1); % dd(:)=D(:,:,k); % hist(dd(:)); % end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Minimum Euclidean Distance %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k= 1:NumWin-1 [NumChan NumSamples ]= size(data); min_value = zeros(NumChan ,NumSamples); min_index= zeros(NumChan, NumSamples); [min_value min_index]= min(B(:,:)); end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Sorting of distance value %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k= 1:NumWin-1 [NumChan NumSamples ]= size(data);
% min_value(:) = (minxy(minxy(B(:,:))); % min_index(:,:)=minxy(B(:,:)); [sort_val ,pos] = sort(B(:,:),dim,ascend); end % %
I am already post the code. Main problematic part is sorting the distance value .Till mixing matrix it is completely okay .
I also read your blog .I t is pretty impressive. Univerisät of heildelberg ist sehr schöne universität :)
I already post the whole code... my probelm is only in the last part.Did you figure out something???
I am really waiting for your answer
@Riya: I do not have a blog, so I'm not sure what you have read. The University still keeps a very old homepage of mine.
You code is hard to read. I've posted instructions how to format the code properly, so it would be a reader-friendly idea to follow it. Currently the posted code is nearly useless due to the missing readability. The huge comment blocks decrease the readability also.
The brute header clear all; close all; warning off; is something I cannot warn for enough. A mercyless killing of existing data, loaded functions and the possibility to warn is a bad programming habit.
Instead of if(isunix); separator = '/'; else separator = '\'; end you might prefer the built-in filesep command.
I hope you can reconsider, that I cannot imagine, which parts of your code are meant by: "Main problematic part is sorting the distance value. Till mixing matrix it is completely okay." What exactly is the "last part" and which problems do it cause exactly?

Sign in to comment.

More Answers (0)

Products

Tags

Asked:

on 18 Mar 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!