Filter löschen
Filter löschen

if i have some matrix how can i do this operation on it ?

1 Ansicht (letzte 30 Tage)
if i have matrix where the size of it is (2,5) and i have a matrix
a = [ 1 3
3 1 ]
* where a matrix show how many of ones will be in the f matrix and between these group of ones just one zero
how to generate matrix like this
f = [ 1 0 1 1 1
1 1 1 0 1 ]

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 29 Apr. 2016
This is not the best way, but it works
a = [ 1 3
3 1 ]
b=arrayfun(@(x) [0 ones(1,x)],a,'un',0)
c=arrayfun(@(x) cell2mat(b(x,:)),(1:size(a,1))','un',0)
d=cell2mat(c);
out=d(:,2:end)
  2 Kommentare
Firas Al-Kharabsheh
Firas Al-Kharabsheh am 29 Apr. 2016
if i have this code to search about the row which make the if statement is true and put it in F matrix
a = [3 2 0
2 1 1
2 1 1
2 1 0
3 2 0
4 3 0
5 4 0
4 3 0
4 3 0
10 0 0 ]
a1=sum(a,2)
b1=sum(a~=0,2)
n1 = 10
m1 = 10
[n,m]=size(a)
F = zeros(n1,m1)
for k=1:n
if 10 == (a1(k) + (b1(k) -1 ))
b=arrayfun(@(x) [0 ones(1,x)],a,'un',0)
c=arrayfun(@(x) cell2mat(b(x,:)),(1:size(a,1))','un',0)
d=cell2mat(c);
out=d(:,2:end)
end
end
the final answer will be
F = [ 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 1 1 1 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 ]
Walter Roberson
Walter Roberson am 29 Apr. 2016
You duplicate your questions too much, which is a pain to clean up after. The volunteers can either spend time answering code or they can spend time cleaning up questions, but they don't have time to do both.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by