Hi everybody. I have two problems. I think they are simple to solve but I cannot at this time!!!, I don't know how!!!
First problem: I constructed a matrix dkc of size (1057 X 11), and many zeros inside. I used this code to remove zeros:
for g=1:11
new_variable = dkc( dkc(:,g)~=0, 2);
end
but I got the resulted matrix in only one column. I want the matrix as it is, 2D with 11 columns, without zeros in the array.
Second problem I have, when I have another column say s with a different length than the previous matrix dkc. Can I check which column in dkc is nearest in its elements with this elements of s? and how? And which method will use?
Thanks for all, and really I need urgently these solutions to complete my research.

 採用された回答

dpb
dpb 2014 年 3 月 20 日
編集済み: dpb 2014 年 3 月 20 日

0 投票

for g=1:11
new_variable = dkc(dkc(:,g)~=0, 2);
end
A) The above replaces new_variable each pass through so what you're left with is the last column for the logical addressing array for the row selection and the second column from the fixed 2.
B) Matlab cannot store "jagged" arrays--to actually not have anything as a placeholder for the zeros the only way you can do this is with a cell array. Or, if you just need an indicator consider substituting NaN -- certain Matlab functions are enabled that they will skip those elements altho certainly that's not universal.
To build the jagged array via your loop solution,
nv=cell(1,11); % an empty cell array of 11 columns
for i=1:11
nv(i)={dkc(dkc(:,i)~=0,i)};
end
NB the curlies "{}" to turn the double vector into a cell array

8 件のコメント

Hany Elsalamony
Hany Elsalamony 2014 年 3 月 20 日
Thank you very much, it is working; but how can i assign the nearest cell from these with the another cell S (of another external unequal array) say 705 x 1, to get which one of them will be nearest. Also what is suitable method to get the best matching?
Thanks again for your great cooperation
Hany Elsalamony
Hany Elsalamony 2014 年 3 月 20 日
Or, matching with another matrix of other cells to get which are nearest to these cells in dkc (one cell by one) or value by value.
dpb
dpb 2014 年 3 月 20 日
Hard to say what would be "best" matching if they're of noncommensurate length -- what would you define as "matching"? Nearest average, minimum maximum difference, ...?
Hany Elsalamony
Hany Elsalamony 2014 年 3 月 21 日
Many thanks for your response, actually, i got an idea, that is extending the shorter array of data or shrinking the longer one to be equal then i can match, but i don't know how i can do this. What is your opinion about this idea? suggest one.
dpb
dpb 2014 年 3 月 21 日
Well, either concatenate to the shorter or select from the longer the subset. The latter would be sotoo
minRows = min(size(x,1), size(y,1)); % minimum rows in the two
x = x(1:minRows, :);
y = y(1:minRows, :);
Hany Elsalamony
Hany Elsalamony 2014 年 3 月 21 日
By this way, will we neglect some elements from the longer to be shorter?And these values eliminated (or cutting) from the end of longer array will not affect on the array??
dpb
dpb 2014 年 3 月 22 日
Yes, the above selects the first N rows where N is the min(size(...,1)) for the two. Yes, it therefore ignores the remainder after that in the longer and they're gone forever into the bit bucket in the sky...
Whether that's an acceptable result on the array or not depends entirely on what your application needs -- we can't answer that, only you can.
Hany Elsalamony
Hany Elsalamony 2014 年 3 月 23 日
Thank you so much for your help, great cooperation, your time, and efforts.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by