compare cell array elements with non zero elements in 2D array

hey i have cell array containing multiple elements e.g.
x{1,1}={2;1}
x{2,1}={1;[1;2]}
and a 2D array:
y=[0,0,-1,1,0,0;-1,0,1,-1,0,0]
for x{1,1}{1,1}=2, it will consider 1st non zero column value (e.g. 3 in 1st row is first non zero column index ) in 1st row of y, and in 2nd row(As x{1,1}{1,1}=2), it will check what value is placed at that non zero index and store it in another cell array. for x{1,1}{2,1} it will consider 2nd non zero element of 1st row.
for x{2,1}{1,1}=1, it will consider 2nd row and check which 1st non zero element.

1 件のコメント

Tha saliem
Tha saliem 2017 年 4 月 11 日
Result would be like this:
result{1,1}{1,1}=1
result{1,1}{2,1}=1
result{2,1}{1,1}=0
result{2,1}{2,1}=[-1;1]

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

 採用された回答

Guillaume
Guillaume 2017 年 4 月 11 日
編集済み: Guillaume 2017 年 4 月 11 日

0 投票

I assume you've made a mistake with your example result. How can you have 0 in the outptu if you only look at the non-zero values. Also, the first non-zero value in row 1 is -1, so I assume that result{1}{2} should be -1.
If so:
result = cell(size(x));
for row = 1:size(x, 1)
ynonzeros = nonzeros(y(row, :));
result{row} = cellfun(@(cols) ynonzeros(cols), x{row}, 'UniformOutput', false);
end
Because of the need of the temporary ynonzeros it's not possible to replace the outer loop with an arrayfun with an anonymous function, as I had in my previous answer.

5 件のコメント

Tha saliem
Tha saliem 2017 年 4 月 13 日
編集済み: Tha saliem 2017 年 4 月 13 日
hey. Thankyou for your help. sorry for not clarifying my question Actually what i want to do is that for example i have y like this:
and elements of x{1,1} are:
x{1,1}{1,1}=4
x{1,1}{2,1}=4
it will take first element of x{1,1} which is 4 and this 4 will indicate the row to be used in y matrix. and also as it is first element of x{1,1} it will consider 1st non zero element of 1st row in y. It means value at column 3 of row 1 will be considered. Now it will check value which is present at 3rd column of 4th row and will display result.
for x{1,1}{2,1}=4, it will consider 2nd non zero element of 1st row in y. and will display value present at 4th column of 4th row.
Tha saliem
Tha saliem 2017 年 4 月 13 日
Similarly if x{2,1}{1,1}=[3,4]
it will consider first non-zero element of 2nd row and which is at column 1, it will then display value present at 1st column of 3rd row and 1st column of 4th row.
Tha saliem
Tha saliem 2017 年 4 月 13 日
your code gives following error im unable to find out whats wrong nothing seems erroneous.
Index exceeds matrix dimensions.
Error in test>@(cols)ynonzeros(cols)
Error in test (line 61) result{row} = cellfun(@(cols) ynonzeros(cols), x{row}, 'UniformOutput', false);% code
Guillaume
Guillaume 2017 年 4 月 13 日
result = cell(size(x));
for row = 1:size(x, 1)
ycols = find(y(row, :));
result{row} = arrayfun(@(r) y(x{row}{r}, ycols(r)), ....
(1:numel(x{row})).', ...
'UniformOutput', false);
end
Tha saliem
Tha saliem 2017 年 4 月 13 日
yes its completely accurate. Thank you so much for help.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2017 年 4 月 11 日

コメント済み:

2017 年 4 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by