How can I find the value in matrix

1 次查看(过去 30 天)
I have a matrix:
M =
NaN 23 37 53 53
NaN 42308.00000 42308.00019 42308.00037 42308.00056
14 0.2998 0.4497 0.0996 0.2495
23 0.2002 0.0503 0.4004 0.2505
48 0.7998 0.9497 0.5996 0.7495
53 0.7002 0.5503 0.9004 0.7505
The first row is the name of columns and the first columns is the names of rows, How can I find the value in M where its column label and row label matched for the whole matrix, the final output should be vector of the values.
The output is the value for example column head 23 and row name 23 I want to get the value 0.2002, and so on for all matches.
23 37 53 53
42308.00000 42308.00037 42308.00056
14
23 0.2002
48
53 0.9004 0.7505
So the output as a 2 vector2 of the following: [0.2002 0.9004 0.7505] and [42308.00000 42308.00037 42308.00056] I am using the following useful script:
[tf,loc] = ismember(M(2:end,1), M(1,2:end));
output = M( sub2ind(size(M), find(tf)+1, loc(tf)+1) );
But, there is no repetition with the row label, while the column label can have repetition, for ex. may have 2 columns label with the same label as 53 could 4 columns with the same label. Many thanks for help!
  2 个评论
Guillaume
Guillaume 2018-7-4
As described, M cannot be constructed in matlab. Can you describe your M using valid matlab syntax?

请先登录,再进行评论。

采纳的回答

Rik
Rik 2018-7-4
The code below should do it.
M=[NaN 23 37 53 53
14 0.2998 0.4497 0.0996 0.2495
23 0.2002 0.0503 0.4004 0.2505
48 0.7998 0.9497 0.5996 0.7495
53 0.7002 0.5503 0.9004 0.7505];
mc=M(1,2:end);
mr=M(2:end,1);
L= mc==mr;%requires implicit expansion
% If you have an older release, use this:
% L= bsxfun(@eq,mc,mr)
%extend L to match the size of M
L=[false(1,size(M,2));
false(size(L,1),1) L];
wanted_vector=M(L)';
  13 个评论
Rik
Rik 2018-7-5
You forgot to select your code and push the {}Code button, so your matrix is unreadable. Therefore, I used the matrix in your question body instead. The code below shows a method to get the vectors you want. It will return a normal vector in case there is only 1 vector, and it will return a cell array if there are multiple vectors as output.
I also appended a display module that prints the vectors with 5 decimals. It shows the structure of the result in the command window, so you can ignore that part if you want.
M=[NaN 23 37 53 53
NaN 42308.00000 42308.00019 42308.00037 42308.00056
14 0.2998 0.4497 0.0996 0.2495
23 0.2002 0.0503 0.4004 0.2505
48 0.7998 0.9497 0.5996 0.7495
53 0.7002 0.5503 0.9004 0.7505];
mc=M(1,2:end);
mr=M(~isnan(M(:,1)),1);
NaN_rows=sum(isnan(M(:,1)));
L= mc==mr;%requires implicit expansion
% If you have an older release, use this:
% L= bsxfun(@eq,mc,mr)
%extend L to match the size of M
L=[false(size(M,1)-size(L,1),size(M,2));
false(size(L,1),1) L];
if NaN_rows==1
wanted_vector=M(L)';
else
wanted_vector=cell(NaN_rows,1);
wanted_vector{1}=M(L)';
L2=any(L,1);
for n=2:NaN_rows
wanted_vector{n}=M(n,L2);
end
end
%display the result:
clc
if isa(wanted_vector,'cell')
for n=1:numel(wanted_vector)
FormatSpec=['wanted_vector{' num2str(n) '}=[ ' ...
repmat('%.5f ',1,numel(wanted_vector{n})) '];\n'];
fprintf(FormatSpec,wanted_vector{n})
end
else
FormatSpec=['wanted_vector=[ ' ...
repmat('%.5f ',1,numel(wanted_vector)) '];\n'];
fprintf(FormatSpec,wanted_vector) %#ok<PFCEL>
end
Rafat Qubaja
Rafat Qubaja 2018-7-5
Dear Brother Rik, Many Many thanks for the great job, you did great, mervelas and magic work, If you please let me know how I can say thank you in this website more than words. Cheers, rafat

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by